In Today article, we will see how to validate password with Regular expression. Its a daily process for web developers to validating the password with certain rules.With java script client side validate can be achieved. Below the code will help you to validate the password which contain at least one Letter , one Number & one special character. The password length must be within a given range.
/* Password must be at least one char, one special char & one digit & the length must be given*/
function validatePassword(pwdelem,alertmsg){
var password_expression=/^((?=.*[a-zA-Z])(?=.*\d)(?=.*[#@%$]).{6,20})$/;
if(pwdelem.value.match(password_expression)){
return true;
}
else{
alert(alertmsg);
pwdelem.focus(); //Set focus to the given source element id ( textbox or password) ,it is optional .
return false;
}
}
/* Password must be at least one char, one special char & one digit & the length must be given*/
function validatePassword(pwdelem,alertmsg){
var password_expression=/^((?=.*[a-zA-Z])(?=.*\d)(?=.*[#@%$]).{6,20})$/;
if(pwdelem.value.match(password_expression)){
return true;
}
else{
alert(alertmsg);
pwdelem.focus(); //Set focus to the given source element id ( textbox or password) ,it is optional .
return false;
}
}
Happy Learning.
No comments:
Post a Comment