var reg=/regular expression/;
Boolean reg.test (the string to validate);
The JS engine converts/Regular expression/"" to a RegExp object, returning True when the string satisfies the requirements of the regular expression.
An expression validation example I wrote: Features are as follows:
User name, cannot be empty
Password 6 is a number
Password confirmation, two passwords must be entered the same password
ID number must be 15 digits, or 18 digits, at the end can also be X (this feature has not been written, there is time to fill in)
Copy Code code as follows:
<! DOCTYPE html>
<title>testTablel.html</title>
<!--user name, cannot be empty
Password 6 is a number
Password confirmation, two passwords must be entered the same password
ID number must be 15 digits, or 18 digits, at the end can also be x-->
<style>
#d1 {
width:400px;
height:250px;
Background-color: #FFE4B5;
margin:40px Auto;
}
#d1_head {
Color:white;
font-size:20px;
font-family: "Arial";
height:24px;
Background-color:bule;
}
#d1_content {
padding-left:30px;
padding-top:30px;
}
. s1 {
color:red;
Font-style:italic;
}
. s2 {
border:2px dotted blue;
}
</style>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<script>
function Check_username () {
var Txtobj=document.getelementbyid ("username");
Txtobj.classname= ';
var Msgobj=docuemnt.getelementbyid ("Username_msg");
Msgobj.innerhtml= ';
if (txtobj.value.length==0) {
Msgobj.innerhtml= ' username cannot be empty ';
Assign a value to a node's class property
Txtobj.classname= ' S2 ';
return false;
}
return true;
}
function Check_pwd () {
var Pwdobj=document.getelementbyid ("pwd");
Pwdobj.classnames= ';
var Msgobj=document.getelementbyid ("Pwd_msg");
Msgobj.innerhtml= ';
var reg=/^\d{6}$/;
A test method in JavaScript that returns a Boolean
if (!reg.test (Pwdobj.value)) {
Msgobj.innerhtml= ' password is 6 digits ';
Pwdobj.classname= ' S2 ';
return false;
}
return true;
}
function Check_pwd1 () {
var Pwdobj=document.getelementbyid ("pwd");
Pwdobj.classnames= ';
var Msgobj=document.getelementbyid ("Pwd_msg");
Msgobj.innerhtml= ';
var Pwdobj1=document.getelementbyid ("Pwd1");
Pwdobj1.classnames= ';
var Msgobj1=document.getelementbyid ("Pwd_msg1");
Msgobj1.innerhtml= ';
if (Pwdobj.value!=pwdobj1.value) {
Msgobj1.innerhtml= ' passwords are inconsistent, please re-enter ';
return false;
}
return true;
}
function Check_form () {
var flag=check_username () &&check_pwd () &&check_pwd1;
return flag;
}
</script>
<body>
<div id= "D1" >
<div id= "D1_head" > Registration </div>
<div id= "D1_content" >
<form onsubmit= "return Check_form ();" >
<table>
<tr>
<td> User name </td>
<td><input id= "username" name= "username"
Onblur= "Check_username ()"/> <span class= "S1" id= "Username_msg" ></span>
</td>
</tr>
<tr>
<td> Password </td>
<td><input type= "password" id= "pwd" name= "pwd"
Onblur= "check_pwd ()"/> <span class= "S1" id= "Pwd_msg" ></span></td>
</tr>
<tr>
<td> Confirm Password </td>
<td><input type= "Password" id= "pwd1" name= "PWD1"
Onblur= "check_pwd1 ()"/> <span class= "S1" id= "PWD_MSG1" ></span>
<tr>
<TD colspan= "2" >
<input type= "Submit" value= "confirm"/> <input type= "Reset"
value= "Reset"/>
</td>
</tr></table>
</form>
</div>
</div>
</body>