Guest User

Untitled

a guest
Oct 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. <?php
  2.  
  3. $validator = new Validator();
  4.  
  5. // Each validation rule is a property of the validator object.
  6. $validator->username = function($value, $key, $self)
  7. {
  8. if(preg_match('~\W~', $value))
  9. {
  10. return 'Your username must only contain letters and numbers';
  11. }
  12. }
  13.  
  14. $validator->password = function($value, $key, $self)
  15. {
  16. if(strlen($value) < 8)
  17. {
  18. return 'Your password must be at least 8 characters long';
  19. }
  20. };
  21.  
  22.  
  23. // $_POST = array('username' => ..., 'password' => ...)
  24. if( ! $validator($_POST))
  25. {
  26. // Return a JSON object with the validation errors (great for jQuery)
  27. die(json_encode($validator->errors()));
  28. }
  29. else
  30. {
  31. // Save new user to database
  32. }
Add Comment
Please, Sign In to add comment