I would like to validate passwor开发者_StackOverflow中文版d in password field with say minimum 6 characters and maximum 12. and also it should be alphanumeric character. how can I validate the same with PHP?? and also need to validate email too..
Validating a password is often a weird thing to do, because your storage should be hashed or encrypted, so you never actually see the original password (thus why limiting character ranges and maximum length is unnecessary).
Limiting it to these characters will only make cracking them easier (subset of characters to test).
You can validate that using a regular expression.
preg_match('/^\w{6,12}$/', $str);
(this allows underscores (_
) as well).
A better validating would be a minimum length (8 chars in this example) and perhaps a non alphanumeric character, which would be...
strlen($str) >= 8 AND preg_match('/\W/', $str)
You can validate an email using...
filter_var($str, FILTER_VALIDATE_EMAIL);
精彩评论