您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关js输入框如何使用正则表达式校验输入内容,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
源码如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> 用户名:<input type="text"name = "user" id="user"/> <span id="userSpan" ></span> <br /> 密码:<input type="text"name = "pwd" id="pwd"/> <span id="pwdSpan" ></span> <br /> 确认密码:<input type="text"name = "pwd2" id="pwd2"/> <span id="pwd2Span" ></span> <br /> 邮箱:<input type="text"name = "email" id="email"/> <span id="emailSpan" ></span> <br /> 手机号:<input type="text"name = "phone" id="phone"/> <span id="phoneSpan" ></span> <br /> 身份证号:<input type="text"name = "status" id="status"/> <span id="statusSpan" ></span> <br /> 地址:<input type="text"name = "address" id="address"/> <span id="addressSpan" ></span> <br /> <input type="button" value="校验" onclick="verify()" /> <script type="text/javascript"> function verify() { //用户名验证 var user = ""; user = document.getElementById("user").value; var pattl = /^[A-Z][a-zA-Z0-9_]{6,20}$/; var result = pattl.test(user.trim()); if (result) { document.getElementById("userSpan").innerHTML = "√" document.getElementById("userSpan").style.fontSize = "20pt" } else { document.getElementById("userSpan").innerHTML = "×" } //密码验证 var pwd = ""; pwd = document.getElementById("pwd").value; var pattl = /^[A-Z][a-zA-Z0-9_]{8,15}$/; var result = pattl.test(pwd.trim()); if (result) { document.getElementById("pwdSpan").innerHTML = "√" document.getElementById("pwdSpan").style.fontSize = "20pt" } else { document.getElementById("pwdSpan").innerHTML = "×" } //确认密码验证 var pwds = ""; pwds = document.getElementById("pwd2").value; var pattl = /^[A-Z][a-zA-Z0-9_]{8,15}$/; var result = pattl.test(pwds.trim()); if (result) { document.getElementById("pwd2Span").innerHTML = "√" document.getElementById("pwd2Span").style.fontSize = "20pt" } else { document.getElementById("pwd2Span").innerHTML = "×" } //邮箱验证 var emails = ""; emails = document.getElementById("email").value; var pattl = /^[A-z0-9]+@[a-z0-9]+.com$/; var result = pattl.test(emails.trim()); if (result) { document.getElementById("emailSpan").innerHTML = "√" document.getElementById("emailSpan").style.fontSize = "20pt" } else { document.getElementById("emailSpan").innerHTML = "×" } //手机号验证 var phones = ""; phones = document.getElementById("phone").value; var pattl = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/; var result = pattl.test(phones.trim()); if (result) { document.getElementById("phoneSpan").innerHTML = "√" document.getElementById("phoneSpan").style.fontSize = "20pt" } else { document.getElementById("phoneSpan").innerHTML = "×" } //身份证号验证 var status1 = ""; status1 = document.getElementById("status").value; var pattl = /^([1-9]){1}[0-9]{17}|[1-9]{1}[0-9]{16}(x|X)?$/; var result = pattl.test(status1.trim()); if (result) { document.getElementById("statusSpan").innerHTML = "√" document.getElementById("statusSpan").style.fontSize = "20pt" } else { document.getElementById("statusSpan").innerHTML = "×" } //地址验证 var addres = ""; addres = document.getElementById("address").value; var pattl = /^[\u4e00-\u9fa5]+[\u4E00-\u9FA5A-Za-z0-9_]+$/; var result = pattl.test(addres.trim()); if (result) { document.getElementById("addressSpan").innerHTML = "√" document.getElementById("addressSpan").style.fontSize = "20pt" } else { document.getElementById("addressSpan").innerHTML = "×" } } </script> </body> </html>
效果图如下:
关于“js输入框如何使用正则表达式校验输入内容”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。