Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  35] [ 7]  / answers: 1 / hits: 7146  / 10 Years ago, thu, june 5, 2014, 12:00:00

I need to add some further validation to this password code. I created a if statement where if any of the conditions fail, the condition that failed will print out the relevant message. I tried adding validation for:




  • Password must have 1 Digit (0-9)

  • Password must have 1 UpperCase (A-Z)

  • Password must have 1 LowerCase (a-z)



Javascript code:



<script type=text/javascript>
function check_form() {
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;

if (passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw)) {
if (passw.length < 6) {
alert(Please make sure password is longer than 6 characters.)
return false;
}
if (passw != passw2) {
alert(Please make sure passwords match.)
return false;
}
if (!letter.test(passw)) {
alert(Please make sure Password Includes an UpperCase and LowerCase character)
return false;
}
if (!number.test(passw)) {
alert(Please make sure Password Includes a Digit)
return false;
}

/*email test*/
var email = document.getElementById('email-input-0').value;
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email)) {
alert('Please provide a valid email address');
form.email.focus;
return false;
}


return true;
}
}
</script>


This code doesn't seem to work and being an inexperience JS programmer I'm not sure why it's not working. I've left the email-validation just to make sure the fix doesn't interfere with it. Thanks for reading!



Fiddle


More From » javascript

 Answers
0

There were a few problems in your code. First, you were missing a couple of } close braces - for example the end of your first main if clause.



Secondly, the character check was a bit bugged - it checks both uppercase and lowercase in the same check, so passwords that shouldn't be valid got through.



I have updated the fiddle and it now contains the right code, solving both problems: http://jsfiddle.net/3kPkQ/3/



function check_form()
{
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-z]/;
var upper =/[A-Z]/;
var number = /[0-9]/;

if(passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw) || !upper.test(passw)) {
if(passw.length<6){
alert(Please make sure password is longer than 6 characters.)
return false;
}
if(passw != passw2){
alert(Please make sure passwords match.)
return false;
}
if(!letter.test(passw)){
alert(Please make sure password includes a lowercase letter.)
return false;
}
if(!number.test(passw)){
alert(Please make sure Password Includes a Digit)
return false;
}
if(!upper.test(passw)) {
alert(Please make sure password includes an uppercase letter.);
return false;
}
}

/*email test*/
var email = document.getElementById('email-input-0').value;
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email)) {
alert('Please provide a valid email address');
form.email.focus;
return false;
}


return true;
}


That should work - fixes the second problem in @faby's answer too.


[#44805] Wednesday, June 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;