Guest User

Untitled

a guest
Jan 12th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. Make sure username contains only: A-Z, a-z, ., _, -, 0-9 (no comma) PHP [closed]
  2. if(preg_match("/[^w-.?]/", $user)){
  3. // invalid character
  4. }
  5. if(!filter_var($email, FILTER_VALIDATE_EMAIL) || strstr($email,'@yahoo.com')) {
  6. // either invalid email, or it contains in @yahoo.com
  7. }
  8.  
  9. <?php
  10.  
  11. // The validator class
  12.  
  13. class Validator
  14. {
  15. public function isValidUsername($username)
  16. {
  17. if(preg_match('/^[a-zA-Z0-9_-.]+$/', $username)) {
  18. return true;
  19. }
  20. return false;
  21. }
  22.  
  23. public function isYahooMail($mail) {
  24. if(preg_match('/^[a-zA-Z0-9_-.]+@yahoo.com$/', $mail)) {
  25. return true;
  26. }
  27. return false;
  28. }
  29. }
  30.  
  31. // The way to use this class
  32.  
  33. $username = "otporan_123";
  34. $email = "otporan@gmail.com";
  35.  
  36. $badUsername = "otporan*bad";
  37. $yahooEmail = "otporan@yahoo.com";
  38.  
  39. $validator = new Validator();
  40.  
  41. var_export($validator->isValidUsername($username));
  42. echo "<br />";
  43.  
  44. var_export($validator->isValidUsername($badUsername));
  45. echo "<br />";
  46.  
  47. var_export($validator->isYahooMail($email));
  48. echo "<br />";
  49.  
  50. var_export($validator->isYahooMail($yahooEmail));
  51. echo "<br />";
  52.  
  53. ?>
  54.  
  55. if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
  56. // there are one or more of the forbidden characters (the set of which is unknown)
  57. }
  58.  
  59. if (!preg_match('/w-/', $username) {
  60. //throw error
  61. }
Add Comment
Please, Sign In to add comment