Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $validator = new Validator();
- // Each validation rule is a property of the validator object.
- $validator->username = function($value, $key, $self)
- {
- if(preg_match('~\W~', $value))
- {
- return 'Your username must only contain letters and numbers';
- }
- }
- $validator->password = function($value, $key, $self)
- {
- if(strlen($value) < 8)
- {
- return 'Your password must be at least 8 characters long';
- }
- };
- // $_POST = array('username' => ..., 'password' => ...)
- if( ! $validator($_POST))
- {
- // Return a JSON object with the validation errors (great for jQuery)
- die(json_encode($validator->errors()));
- }
- else
- {
- // Save new user to database
- }
Add Comment
Please, Sign In to add comment