Creating Password using following rules: 1. atleast 8 character, 2. atleast one digit, 3. atleast one symbol, 4. atleast one upper case, 5. atleast one lower case

Fill In The Blank
Views 373

Answer:

 
<!DOCTYPE html>
<html>
<body>
    
  <input type="text" id="passwordField"> 
  <p id ="demo"> </p>
<button onclick="validatePassword()"> submit </button> 


<script>    
function validatePassword() {  
     var txt = document.getElementById("passwordField").value;
    // Check for password length 8
    if(txt.length >= 8){
        var patt1 = /[1-9]/g;  
        // Check for Atleast One Digit
        if(txt.match(patt1))
           {
               document.getElementById("demo").innerHTML = "";             
               // Check for special symbol
               var patt8 = /[!@#$%^&*(),.?":{}|<>_]/g; 
               if(txt.match(patt8))
                 {
                  document.getElementById("demo").innerHTML = ""; 
                     var patt2 = /[A-Za-z]/g; 
                // Check for Atleast One Character
               if(txt.match(patt2))
                 {
                      document.getElementById("demo").innerHTML = "";                     
                      // Check for Atleast One Uppercase digit
                     var patt3 = /[A-Z]/g; 
                     if(txt.match(patt3)){
                          document.getElementById("demo").innerHTML = ""; 
                              // Check for Atleast One Lower digit
                               var patt5 = /[a-z]/g; 
                              if(txt.match(patt5)){
                               document.getElementById("demo").innerHTML = "Password Policy Matches";                                
                                  
                             }else{
                               document.getElementById("demo").innerHTML = "Atleast One Lower"; 
                            }
                     }else{
                         document.getElementById("demo").innerHTML = "Atleast One Upper"; 
                     }
                     
                     // Check for Atleast One Lower digit
                     var patt4 = /[a-z]/g; 
                     if(txt.match(patt4)){
                          document.getElementById("demo").innerHTML = ""; 
                                 // check for atleast upper digit
                                    var patt6 = /[A-Z]/g; 
                                    if(txt.match(patt6)){
                                      document.getElementById("demo").innerHTML = "Password Policy Matches";     
                                    }else{
                              document.getElementById("demo").innerHTML = "Atleast One Upper"; 
                     }
                     }else{
                         document.getElementById("demo").innerHTML = "Atleast One Lower"; 
                     }
                 }
                 else{
                   document.getElementById("demo").innerHTML = "Atleast One lower and one Upper Character"; 
               } 
               
                 }
               else{
                document.getElementById("demo").innerHTML = "Atleast One Symbol"; 
           }    
               
           }
           else{
            document.getElementById("demo").innerHTML = "Atleast One Digit"; 
           }
     }else
     {
      document.getElementById("demo").innerHTML = "Password not less than 8"; 
    } 
} 
</script>  
    
    
</body>
</html>
    

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of JavaScript, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.