javor Posted April 10, 2015 Share Posted April 10, 2015 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!" } Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted April 22, 2015 Share Posted April 22, 2015 (edited) 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 April 22, 2015 by Drongo_III Quote Link to comment Share on other sites More sharing options...
maxxd Posted April 23, 2015 Share Posted April 23, 2015 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.