3

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

6
  • Uh... I think this code if(passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw) ) {...} is kinda redundant, because you're checking again inside it. Commented Jun 5, 2014 at 9:01
  • Please post a JSFiddle of your code so we can easier/faster take a look at it Commented Jun 5, 2014 at 9:01
  • @user3241019 fiddle is there, but I did add it late Commented Jun 5, 2014 at 9:03
  • @dunli how would you recommend doing it? Would it be better to split it all up into individual If statements? Commented Jun 5, 2014 at 9:04
  • Ah yes thanks, I was actually working on a new one for you Commented Jun 5, 2014 at 9:04

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm! Can't believe I missed the }!! Thank you so much :)
0

working fiddle

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

   function check_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]/;
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) || !letterUp.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;
}
}

html

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

3 Comments

I'll test this now on the live website
It works OK, but if you put in helloworld9 as the password it won't ask you to include an uppercase letter, or vice versa for HELLOWORLD9.
update.. try now and let me know. I'm sorry I didn't saw that you had already a correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.