Javascript implements two methods-javascript

Source: Internet
Author: User
This article mainly introduces two methods for implementing at least the password level of characters including letters, uppercase and lowercase numbers in JavaScript, which can effectively detect the user password level and provide some reference value, for more information about how to use JavaScript code, see the following example. Share it with you for your reference. The details are as follows:

Preface

Password. if it is too simple to be set, it will be easily broken. Therefore, many websites set strict requirements for password settings. generally, 2 is selected for letters, numbers, and characters, case sensitive. If the password is too simple, an error is prompted. Or display the password level (low, medium, and high) so that you can set an advanced password. So how can we use JS to implement it?

The implementation code is as follows:

Function passwordLevel (password) {var Modes = 0; for (I = 0; I <password. length; I ++) {Modes | = CharMode (password. charCodeAt (I);} return bitTotal (Modes); // CharMode function CharMode (iN) {if (iN> = 48 & iN <= 57) // Number return 1; if (iN> = 65 & iN <= 90) // capital letter return 2; if (iN> = 97 & iN <= 122) | (iN> = 65 & iN <= 90) // case return 4; else return 8; // special character} // bitTotal function bitTotal (num) {modes = 0; for (I = 0; I <4; I ++) {if (num & 1) modes ++; num >>>= 1 ;} return modes ;}}

Use

Common use

Syntax: passwordLevel (string)
Verification rules: numbers, upper-case letters, lower-case letters, and special characters
Function result: returns the number of rules contained in the password.
Example:

PasswordLevel ("123456") // returns 1 passwordLevel ("Abc '123456") // returns 4

Combined with jquery. validate. js:

// Add verification method: contains at least two rules $. validator. addMethod ("strongPsw", function (value, element) {if (passwordLevel (value) = 1) {returnfalse;} returntrue}, "the format does not match "); // start verification $ (". form "). validate ({rules: {pwd: {required: true, // required minlength: 6, // minimum length maxlength: 32, // maximum length strongPsw: true, // password strength}, repwd: {required: true, minlength: 6, maxlength: 32, failed to: "# pwd" // enter the password again }}});

For more information about password generation and strength detection, see online tools:

Online password security detection

High-strength password generator

Online generation tool for short URL

I hope this article will help you design javascript programs.

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.