String verification example using regular expressions in javascript

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.