Validate (input) password


/ Published in: PHP
Save to your folder(s)

This is a useful function if you need to validate a password (input). If you have a form and you need to check if the username, which is registering, entered a valid password (without illegal characters) then this would help you to do it. Only letters, numbers and underscores are accepted.


Copy this code and paste it in your HTML
  1. # <?php
  2. # /*
  3. # Credits: http://www.bitrepository.com/
  4. # */
  5. #
  6. # // default chars values are defined
  7. # function validate_password($password, $min_char = 4, $max_char = 20)
  8. # {
  9. # // Remove whitespaces from the beginning and end of a string
  10. # $password = trim($password);
  11. #
  12. # // Accept only letters, numbers and underscore
  13. # $eregi = eregi_replace('([a-zA-Z0-9_]{'.$min_char.','.$max_char.'})',
  14. # '', $password);
  15. #
  16. # if(empty($eregi))
  17. # {
  18. # return true;
  19. # }
  20. # else
  21. # {
  22. # return false;
  23. # }
  24. # }
  25. #
  26. # $password = 'my_password_123';
  27. #
  28. # /*
  29. # First parameter: Password
  30. # Second parameter: Minimum chars
  31. # Third parameter: Maximum chars
  32. # */
  33. #
  34. # $validator = validate_password($password, 4, 20);
  35. #
  36. # if($validator)
  37. # {
  38. # // Our current example will return true
  39. # echo 'The password is valid.';
  40. # }
  41. # else
  42. # {
  43. # /* if our password value is for instance: [email protected]#%_password
  44. # our function will return false */
  45. # echo 'The password appears to be invalid.
  46. # It should contain only letters, numbers and underscore.';
  47. # }
  48. # ?>

URL: http://www.bitrepository.com/web-programming/php/validate-input-password.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.