user695575
user695575

Reputation: 127

Password Validation - Javascript

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 !!

Upvotes: 1

Views: 3456

Answers (2)

sv_in
sv_in

Reputation: 14049

could you try this regular expression?

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

Upvotes: 1

Pointy
Pointy

Reputation: 413966

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.

Upvotes: 4

Related Questions