javascript实现文本框标签验证的实例代码
程序员文章站
2023-02-16 14:15:37
具体代码如下所述:
< !doctype html >
具体代码如下所述:
< !doctype html > <html lang = "en" > <head > <meta charset = "utf-8" > <title > document < /title> </head > <style type = "text/css" > body { background: #ccc; } label { width: 100px; display: inline - block; } span { color: red; }.container { margin: 100px auto; width: 400px; padding: 50px; line - height: 40px; } span { margin - left: 30px; font - size: 12px; } < /style> <body> <div class="container"> <label>姓名不能为空</label > <input type = "text"id = "inp1" > <span > </span><br/ > <label > phone不能为空 < /label><input type="text" id="inp2"><span></span > <br / ></div> <script> / / 失去焦点后判断用户输入是否为空 var inp1 = document.getelementbyid("inp1"); inp1.onblur = function() { if (trim(this.value) === "") { alert("输入不能为空"); } else { alert("输入正确"); } }; function trim(str) { return str.replace(/^\s+|\s+$/g, ""); } var inp2 = document.getelementbyid("inp2"); inp2.onblur = function(e) { if (inp2.value == "4444") { alert("正确"); e.stoppropagation(); } else { alert("错误") } }; < /script> </body > </html>
总结
以上所述是小编给大家介绍的javascript实现文本框标签验证的实例代码,希望对大家有所帮助
下一篇: 大枣的贮存,一起打枣的童年一去不复返了呀