Jump to content

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!"
            }
            
Link to comment
https://forums.phpfreaks.com/topic/295408-javascript-and-password-verification/
Share on other sites

  • 2 weeks later...

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.