An example of expression verification I wrote: The function is as follows: User Name, cannot be blank; password 6 is a number; Password confirmation, two inputs must have the same password; ID card number must be 15 characters, or 18 bits. The end can also be X var reg =/regular expression /;
Boolean reg. test (string to be verified );
The js engine converts/Regular Expressions/"" into a RegExp object. If the string meets the requirements of regular expressions, true is returned.
An example of expression verification I wrote: The function is as follows:
User name, cannot be blank
Password 6 is a number
Password confirmation. The two passwords must be the same
The ID card number must be 15 or 18 digits, and the end can also be X (this function has not been written yet and can be supplemented with time)
TestTablel.html
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 = 'user name cannot be blank '; // assign txtObj to the class attribute of the node. 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, returns boolean if (! Reg. test (pwdObj. value) {msgObj. innerHTML = 'password is a 6-digit password'; 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.inn ErHTML = ''; if (pwdObj. value! = PwdObj1.value) {msgObj1.innerHTML = 'inconsistent passwords. Enter '; return false;} return true;} function check_form () {var flag = check_username () & check_pwd () & check_pwd1; return flag;} script
Register
For more articles about string verification examples using regular expressions in javascript, refer to PHP Chinese website!