JavaScript fun: password verification

Source: Internet
Author: User
Regular Expressions are undoubtedly the first choice for this type of verification problem. However, if regular expressions are not required, they can be written into the verification logic. For this problem, we divide it into two checks: You have to verify a piece of password to confirm that it meets the following conditions:

1. At least 6 characters in length

2. At least one uppercase letter

3. At least one lowercase letter

4. At least one number

5. There are no special characters except 2, 3, and 4, that is, only letters and numbers are contained.

Regular Expressions are undoubtedly the first choice for this type of verification problem. However, if regular expressions are not required, they can be written into the verification logic.

For this problem, we divide it into two checks:

Establish length detection according to the first requirement.

var lengthValid = function(pass){      return pass.length >= 6;  };

Based on, a content detection function is created.


The logic is as follows: count the number of uppercase and lowercase letters and numbers in the password string. If a special symbol is encountered, false is returned directly.

var contentValid = function(pass){      var lowerNum = 0;      var upperNum = 0;      var numNum = 0;      for(var i=0;i
 
  = 48 && code <= 57){              numNum++;          }          else if(code >= 65 && code <= 90){              upperNum++;          }          else if(code >= 97 && code <= 122){              lowerNum++;          }          else{              return false;          }      }      return lowerNum && upperNum && numNum;  };
 

Finally, the length Detection and content detection are integrated to form a password verification function:

function validate(password) {      return lengthValid(password) && contentValid(password);  }

The above is the JavaScript interesting question: password verification content. For more information, please follow the PHP Chinese Network (www.php1.cn )!

Related Article

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.