Regular Expression for Password [duplicate]
Possible Duplicate:
Regular expression to check if a given passwor开发者_StackOverflow社区d contains at least one number and one letter in c#?
I need to create regular expression for password with following requirement:
Min 2 Small char
Min 2 Caps char Min 1 Special char (!,@,#,$,%,^,&,*,(,),~) Min 2 numeric Min Length 8 charsCan anyone give the regular expression for above specification?
Note: I have already implemented this without Regex using different logic. But, regular expressions are powerful than the manual processing.
Regular expressions aren't particularly well suited for this type of task.
You could probably solve it, possibly using zero-width assertions / look arounds. However, it seems to me like you're after validating some users choice of password.
Even if you did come up with a match / no match regular expression, how would you provide useful feed-back to the user if the password didn't match the expression? Would you say "The password you entered does not conform to these five constraints.....". Wouldn't it be much nicer if the user was told something like "Your password must be at least 8 characters".
If you are indeed validating some users input, it sounds to me like you would be better off checking each constraint one by one.
the function that i use is this.
function check_pass_strength($pwd) {
if( strlen($pwd) > 20 ) {
$error .= "Password too long! <br />";
}
if( strlen($pwd) < 8 ) {
$error .= "Password too short , minimum 8 characters! <br />";
}
if( !preg_match("#[0-9]+#", $pwd) ) {
$error .= "Password must include at least one number! <br />";
}
if( !preg_match("#[a-z]+#", $pwd) ) {
$error .= "Password must include at least one letter! <br />";
}
if( !preg_match("#[A-Z]+#", $pwd) ) {
$error .= "Password must include at least one CAPS! <br />";
}
if( !preg_match("#\W+#", $pwd) ) {
$error .= "Password must include at least one symbol! <br />";
}
if($error){
echo "Password validation failure(your choise is weak):<br /> $error";
return 0;
} else {
return 1;
}
} you can modify it for your needs and voila!!
精彩评论