时间:2021-05-26
源码如下所示:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> 用户名:<input type="text"name = "user" id="user"/> <span id="userSpan" style="font-size: 16pt;"></span> <br /> 密码:<input type="text"name = "pwd" id="pwd"/> <span id="pwdSpan" style="font-size: 16pt;"></span> <br /> 确认密码:<input type="text"name = "pwd2" id="pwd2"/> <span id="pwd2Span" style="font-size: 16pt;"></span> <br /> 邮箱:<input type="text"name = "email" id="email"/> <span id="emailSpan" style="font-size: 16pt;"></span> <br /> 手机号:<input type="text"name = "phone" id="phone"/> <span id="phoneSpan" style="font-size: 16pt;"></span> <br /> 身份证号:<input type="text"name = "status" id="status"/> <span id="statusSpan" style="font-size: 16pt;"></span> <br /> 地址:<input type="text"name = "address" id="address"/> <span id="addressSpan" style="font-size: 16pt;"></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输入框使用正则表达式校验输入内容的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希
望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
js中的正则表达式比起C#中的正则表达式要弱很多,但基本够用了1定义正则表达式2关于验证的三个这则表达式方法3正则表达式式的转义字符1定义正则表达式在js中定义
本教程旨在帮助你驾驭Java正则表达式,同时也帮助我复习正则表达式。什么是正则表达式?正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则
开发中如果有input输入框,难免就要写正则表达式,因此在这里总结了一些常见常用的正则表达式的书写方法,希望会大家有所帮助;附图:一.手机号码$(documen
1、正则表达式包括两部分①定义正则表达式的规则;②正则表达式的模式(i/g/m);2、声明正则表达式①字面量声明:varreg=/表达式规则/表达式模式; e
复制代码代码如下:正则表达式30分钟入门教程正则表达式基础资料