1

I want to validate password field. I have tried following code, but getting message that "Your password must satisfy the following...............", even though i enter a correct password format.

var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$/;

     if ( !re.test(rpassword) )
       {
       alert('Your password must satisfy the following. \n\n* Password should be 4 to 8 character long. \n* Password should have at least one alphabet. \n* Password should have at least one numeric value. \n* Password should  have special characters.');
       return false;
  }   

Not getting what is wrong with this code. Please help !!

6
  • 1
    Where does the variable "rpassword" come from? Commented Apr 28, 2011 at 17:13
  • 1
    What password are you trying? First thing to figure out is whether your password gets passed the regex. Commented Apr 28, 2011 at 17:13
  • "password" is password field on my form. I used function checkForm() { var rpassword; rpassword=password; } Commented Apr 28, 2011 at 17:20
  • @JAA Is that generated output? Commented Apr 28, 2011 at 17:21
  • @User729776 I dont get your comment... your checkForm() function is not going to have the same constraints as the OP Commented Apr 28, 2011 at 17:23

2 Answers 2

4

Are you sure you don't want:

if ( !re.test(rpassword.value) ) {
  // ...
}

?? You say that "rpassword" is a reference to the password element in your form. If so, then you have to get its "value" attribute first.

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

Comments

1

could you try this regular expression?

/^[a-zA-Z0-9!@#$%^&*]{4,8}$/

Comments

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.