Skip to content Skip to sidebar Skip to footer

Add Conditions To Password Validation Javascript

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 mes

Solution 1:

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/

functioncheck_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]/;
varnumber = /[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.")
    returnfalse;
  }
  if(passw != passw2){
    alert("Please make sure passwords match.")
    returnfalse;
  }
  if(!letter.test(passw)){
    alert("Please make sure password includes a lowercase letter.")
    returnfalse;
  }
  if(!number.test(passw)){
    alert("Please make sure Password Includes a Digit")
    returnfalse;     
  }
  if(!upper.test(passw)) {
    alert("Please make sure password includes an uppercase letter.");
    returnfalse;
  }
}

/*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;
  returnfalse;
}


returntrue;
}

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

Solution 2:

working fiddle

http://jsfiddle.net/3kPkQ/5/

functioncheck_form()
{
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-z]/;
 var letterUp= /[A-Z]/;
varnumber = /[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.")
      returnfalse;
      }
      if(passw != passw2){
      alert("Please make sure passwords match.")
      returnfalse;
      }
      if(!letter.test(passw) || !letterUp.test(passw)){
      alert("Please make sure Password Includes an UpperCase and LowerCase character")
      returnfalse;
      }
      if(!number.test(passw)){
      alert("Please make sure Password Includes a Digit")
      returnfalse;     
  }

  /*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;
     returnfalse;
}


returntrue;
}
}

html

<inputtype="text"id="password-input-0"/>
<inputtype="text"id="password-input-1"/>
<inputtype="button" value="clickme"id="password-input-2" onclick="check_form()"/>

Post a Comment for "Add Conditions To Password Validation Javascript"