Jump to content
Sign in to follow this  
javor

javascript and password verification

Recommended Posts

Hello! I am very new to coding, and need some help in getting javascript to verify whether a password has upper- and lowercase letters, and a number. I've already written a part where it checks if the password matches the second time you write it, but for the life of me I can't get the other shit to work.

Here is what I have in the scripting department so far: 

 

     {
            
            var password1 = document.getElementById('password1');
            var password2 = document.getElementById('password2');
                var message = document.getElementById('confirmMessage');
                var goodColor = "#66cc66";
                var badColor = "#ff6666";
            
            if(password1.value == password2.value){
                password2.style.backgroundColor = goodColor;
                message.style.color = goodColor;
                message.innerHTML = "Passwords Match!"
            }else{
                password2.style.backgroundColor = badColor;
                message.style.color = badColor;
                message.innerHTML = "Passwords Do Not Match!"
            }
            

Share this post


Link to post
Share on other sites

Firstly I wouldn't rely on client side JavaScript alone for verifying a password - you'll want to validate server side too.

 

In order to check for the presence of uppercase/lowercase/number you can use a function something similar to the one I've jotted below. This uses a simple regular expression to test a password string against different patterns.

 

 You can easily adapt this to also count the number of instances of a particular character as the 'result' variable will get populated with each individual instance of either an uppercase letter, lowercase letter or number. So you can check result.length to see if the password provided meets your criteria.

 

Hope this helps get you on the right path.


<script>

function testPassword(type, password){

var result,
patterns = {
uppercase : /[A-Z]{1,1}/g,
lowercase : /[a-z]{1,1}/g,
number : /[0-9]{1,1}/g,
}

//pass value of match to result - it becomes null if no match is found
result = password.match(patterns[type]);

//return true if a match is found or false if not
return result === null ? false : true ;

}

var password = "AAcc";

console.log( testPassword('uppercase', password) ); //returns true as the password string has caps
console.log( testPassword('lowercase', password) ); //returns true as there are lowercase characters
console.log( testPassword('number', password) );  //returns false - no digits in the password str



</script>
Edited by Drongo_III

Share this post


Link to post
Share on other sites

If you Google 'javascript password strength meter' you'll find several things already in development. I've not used any of them, but this one looks like it could work well...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.