Password level: At least two JS implementations with letters, uppercase and lowercase numbers, and characters

Source: Internet
Author: User

Objective

Password, if set too simple, it is easy to be compromised, so many sites will set the password requirements are very strict, usually letters, numbers, characters 3 selected 2, case-sensitive. For passwords that are too simple to set, give them a wrong hint. or the password level (Low medium high) display, let the user set the advanced password. So how to use JS to achieve it?

Code

Links: https://gist.github.com/xuanfeng/a44f20cb4569d5b4cd5e

1234567891011121314151617181920212223242526272829 function passwordlevel(password) {     var modes = 0     for (i = Span class= "CRAYON-CN" >0; i < password. Length; i++) { Modes |= charmode(password. charCodeAt(i));     } return bittotal(Modes); //charmode function function charmode(in) { if (in >=) && in <= 57< c17>)//Digital return 1; if (in >= && in <= 90< c17>) //capital Letter return 2; If ((Inch >= 97 && iN <= 122 | | (in >= 65 && in <= 90) //case return 4; Else return 8; //special characters     } //bittotal function function bittotal(num) { modes = 0;         for (i = 0; i < 4< Span class= "Crayon-sy" >; i++) { /span> if (num & 1) modes+ +; num >>>= 1;         } return modes;     }}

Use normal use

Use Syntax: Passwordlevel (String)

Validation rules: Numeric, uppercase, lowercase, special characters

Function Result: Returns the number of rules contained in the password

Running Examples:

12 passwordlevel("123456") //return 1 passwordlevel("ABC ' 123456") //Return 4

Combined with Jquery.validate.js

123456789101112131415161718192021222324 //Add validation method: Contains at least two rules$. Validator. Addmethod ( "STRONGPSW" function (value< Span class= "Crayon-sy" >, element) { if(passwordlevel(value) = = 1){return false ; } return true }, "format not Compliant"); //start validation$(". Form"). Validate({ rules: { pwd: { required: true, //Required minlength: 6, //min. length maxlength: max. //Maximum length strongpsw: true, //Password strength         }, repwd: { required: true, minlength: 6, maxlength: equalto: "#pwd" //re-fill the password to be consistent         }    }});

Password level: At least two JS implementations with letters, uppercase and lowercase numbers, and characters

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.