JS正则表达式 简单应用
知识点:
先生成一个正则规则的对象,使用test()对传入的字符串进行验证,返回布尔类型
代码:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>JS常用正则表达式</title>
</head>
<body>
<pre>
姓 名:<input id="name" onblur="regname()"> <span id="ts2"></span>
身份证号:<input id="sfz" onblur="regsfz()"> <span id="ts3"></span>
手 机 号:<input id="cellphone" onblur="regcellphone()"> <span id="ts4"></span>
邮箱地址:<input id="email" onblur="regemail()"> <span id="ts5"></span>
密 码:<input type="password" id="password" onblur="regpassword()"> <span id="ts6"></span>
提 示:<span id="ts" style="color:orange;">请正确填写表单</span>
</pre>
</body>
</html>
<script>
var ts = document.getElementById("ts");
var ts2 = document.getElementById("ts2");
var ts3 = document.getElementById("ts3");
var ts4 = document.getElementById("ts4");
var ts5 = document.getElementById("ts5");
var ts6 = document.getElementById("ts6");
//检测姓名
function regname(){
var name = document.getElementById("name").value;
var regname = /^[\u4E00-\u9FA5]+$/;
if(name == "" || name == null){
ts.innerHTML = "姓名栏未输入任何内容";
ts.style = "color:red";
ts2.innerHTML = "错误";
ts2.style = "color:red";
//这种写法实际上是对象赋值,因为JS读取变量ts时ts只是一个标签,JS会将其生成一个对象来描述
}else if(!regname.test(name)){
ts.innerHTML = "姓名框内必须输入汉字";
ts.style = "color:red";
ts2.innerHTML = "错误";
ts2.style = "color:red";
}else{
ts.innerHTML = "姓名输入正确";
ts.style = "color:green";
ts2.innerHTML = "正确";
ts2.style = "color:green";
}
}
function regsfz(){
var sfz = document.getElementById("sfz").value;
var regsfz = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; ;
if(sfz == "" || sfz == null){
ts.innerHTML = "身份证栏未输入任何内容";
ts.style = "color:red";
ts3.innerHTML = "错误";
ts3.style = "color:red";
//这种写法实际上是对象赋值,因为JS读取变量ts时ts只是一个标签,JS会将其生成一个对象来描述
}else if(!regsfz.test(sfz)){
ts.innerHTML = "必须输入正确是身份证";
ts.style = "color:red";
ts3.innerHTML = "错误";
ts3.style = "color:red";
}else{
ts.innerHTML = "身份证输入正确";
ts.style = "color:green";
ts3.innerHTML = "正确";
ts3.style = "color:green";
}
}
function regcellphone(){
var cellphone = document.getElementById("cellphone").value;
var regcellphone = /^(1[3-5][0-9]{9})|(15[89][0-9]{8})$/ ;
if(cellphone == "" || cellphone == null){
ts.innerHTML = "手机栏未输入任何内容";
ts.style = "color:red";
ts4.innerHTML = "错误";
ts4.style = "color:red";
//这种写法实际上是对象赋值,因为JS读取变量ts时ts只是一个标签,JS会将其生成一个对象来描述
}else if(!regcellphone.test(cellphone)){
ts.innerHTML = "必须输入正确的手机号";
ts.style = "color:red";
ts4.innerHTML = "错误";
ts4.style = "color:red";
}else{
ts.innerHTML = "手机号输入正确";
ts.style = "color:green";
ts4.innerHTML = "正确";
ts4.style = "color:green";
}
}
function regemail(){
var email = document.getElementById("email").value;
var regemail = /\w@\w*\.\w/;
if(email == "" || email == null){
ts.innerHTML = "邮件号栏未输入任何内容";
ts.style = "color:red";
ts5.innerHTML = "错误";
ts5.style = "color:red";
//这种写法实际上是对象赋值,因为JS读取变量ts时ts只是一个标签,JS会将其生成一个对象来描述
}else if(!regemail.test(email)){
ts.innerHTML = "必须输入正确的邮箱";
ts.style = "color:red";
ts5.innerHTML = "错误";
ts5.style = "color:red";
}else{
ts.innerHTML = "邮箱输入正确";
ts.style = "color:green";
ts5.innerHTML = "正确";
ts5.style = "color:green";
}
}
function regpassword(){
var password = document.getElementById("password").value;
var regpassword = /^[a-zA-Z\d_]{8,}$/ ;
if(password == "" || password == null){
ts.innerHTML = "密码栏未输入任何内容";
ts.style = "color:red";
ts6.innerHTML = "错误";
ts6.style = "color:red";
//这种写法实际上是对象赋值,因为JS读取变量ts时ts只是一个标签,JS会将其生成一个对象来描述
}else if(!regpassword.test(password)){
ts.innerHTML = "必须输入不得少于八位数字或字母组成密码";
ts.style = "color:red";
ts6.innerHTML = "错误";
ts6.style = "color:red";
}else{
ts.innerHTML = "密码输入符合要求";
ts.style = "color:green";
ts6.innerHTML = "正确";
ts6.style = "color:green";
}
}
//常用JS正则表达式:http://www.cnblogs.com/hai-ping/articles/2997538.html
</script>
效果图: