Javascript code to check password complexity | Validation for Strong Password with example

The following Javascript code is used to check whether the given password satisfies the following rules
1. Password should have at least 8 characters.
2. Password should have

        i) at least one upper case letter (A – Z).

        ii) at least one lower case letter(a-z).

        iii) At least one digit (0 – 9) .

        iv) at least one special Characters of !@#$%&*()

Depending upon number of rules satised, we can decide password complexity whether it is strong or medium or weak. (For eg. if all the rules satisfied , then the password is strong , …)

The following code gives alert message if any one of the above condition is not satisfied.
In two ways we can write the javascript code .

Method 1 -> Using Regular Expressions.

Method 2 -> Using simple javascript code.

//Method 1 using REGEX starts


function isStrongPwd1(password) {

        var regExp = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*()]).{8,}/;

        var validPassword = regExp.test(password);

        return validPassword;

} 

//Method 1 ends here

//Method 2  starts heare

function isStrongPwd2(password) {

     var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

     var lowercase = "abcdefghijklmnopqrstuvwxyz";

     var digits = "0123456789";

     var splChars ="!@#$%&*()";

     var ucaseFlag = contains(password, uppercase);

     var lcaseFlag = contains(password, lowercase);

     var digitsFlag = contains(password, digits);

     var splCharsFlag = contains(password, splChars);

     if(password.length>=8 && ucaseFlag && lcaseFlag && digitsFlag && splCharsFlag)
           return true;
     else
           return false;

  }

function contains(password, allowedChars) {

    for (i = 0; i < password.length; i++) {

            var char = password.charAt(i);

             if (allowedChars.indexOf(char) >= 0) { return true; }

         }

     return false;
}

//Method 2 ends here

//Calling function starts here


function submitDetails()

{

      var password=document.testForm.password1.value;

      var message="The password must have atleast 8 chars with one uppercase letter, one lower case letter, one digit and one of !@#$%&amp;*()";

// Calling method1

      if(!isStrongPwd1(password)) {

           alert(message);

           success=false;

       }

// Calling method2

      if(!isStrongPwd2(password)) {

           alert(message);

           success=false;

      }

} 

//Calling function ends here

//Input form for password

<body>
<form name="testForm">
      <input type="password" name="password1" />
      <input type="submit" name="submitb" value="Submit" onClick="javascript:submitDetails()"/>
</form>
</body>

Leave a Reply

elite male extra pills dr phil ed supplements male enhancement bangkok top rated ed pills free male enhancement exercises vimulti male enhancement and duration s viril x male enhancement perieum message penis enlargement what male enhancement are made of strike male sexual enhancement surgical penis enlargement ur medicine primary care healthy man pills superpower sex pills best erectile dysfunction drug best sexual enhancement lubricant rogaine pill tablet sex games male sex enhancement vitamin stack over the counter boner pills new male enhancement surgery male enhancement and anxiety medication penis enlargement precedure erectile dysfunction experimental drugs penis health supplement male enhancement surgery in florida

mango pills for weight loss balanced diet menu for weight loss weight loss pills to speed up metabolism best weight loss pills for men m vbc nm diet meal plans weight loss keto pills and apple cider vinegar diet weight loss pill 20lbs in 1 month how to go on a weight loss diet what weight loss pill can i take with wellbutrin caldoxin weight loss pills weight loss pills okc order prescription weight loss pills elite burn weight loss pills week detox diet for weight loss ckd diet and weight loss serious gluten free diet weight loss weight loss with vecan diet male enhancement pills for lose weight sample paleo diet for weight loss for women are there any pills that work for weight loss healthy eating diet plan for weight loss how to lose weight on plant based diet chicken and broccoli diet for weight loss results dr oz weight loss tablets plant based diet weight loss snoring what pills to lose weight

cbd heart arrhythmia op shops perth cbd california cbd cbd oil roswell ga cbd please pure cbd oil gold cbd oil does nothing amazon charlottes web cbd 5000mg cbd oil cbd oil schedule 1 drug dose of cbd oil cannabis oil autism dosage baileys cbd green health cbd cbd juul pods 1000mg boulder marijuana medical cbd oil menu cbd oil phoenix az cbd hemp pre rolls fda and cbd oil indiana cbd laws cbd white label manufacturers medicinal dreams cbd cbn cbd cannabinoids best cbd product for arthritis guide to using plenty flex cbd oil computicket outlets cape town cbd