Password - uppercase characters JavaScript -
excuse if stupid question. i'm doing web design subject @ uni , stuck. have validate password using javascript ensure has , uppsercase, lowercase, numerical character, , @ least 4 characters.
this code have, it's giving me alerts haven't included characters, when have included them i'm still getting alert. appreciated.
var y = document.forms["logindetails"]["password"].value; if (y.length < 4) { alert("your password needs minimum of 4 characters") } if (y.search[/a-z/i] < 1) { alert("your password needs lower case letter") } if (y.search[/a-z/i] < 1) { alert("your password needs uppser case letter") } if (y.search[/0-9/] < 1) { alert("your password needs number") return false; }
your code had several errors
- comparision should
<0
not<1
(search
returns negative value when regexp not found) /i
in regexp (case insensitive - not appropriate when trying figure out upper/lower case characters)- call of search function wrong (usage of
[]
instead of()
) - in regexp
[]
missing ([]
in regexp means 1 character given range,[a-z]
match each lowercase character whereasa-z
match string 'a-z')
it should like:
if (y.length < 4) { alert("your password needs minimum of 4 characters") } else if (y.search(/[a-z]/) < 0) { alert("your password needs lower case letter") } else if(y.search(/[a-z]/) < 0) { alert("your password needs uppser case letter") } else if (y.search(/[0-9]/) < 0) { alert("your password needs number") } else { // pass ok }
Comments
Post a Comment