Comments:"PHP-Validation/examples/jquery.php at master · Dachande663/PHP-Validation · GitHub"
URL:https://github.com/Dachande663/PHP-Validation/blob/master/examples/jquery.php
<?php
include'../autoload.php';
useHybridLogic\Validation\Validator;
useHybridLogic\Validation\Rule;
$validator=newValidator();
$validator
->set_label('name','your name')
->add_filter('name','trim')
->add_rule('name',newRule\NotEmpty())
->add_rule('name',newRule\MinLength(5))
->add_rule('name',newRule\MaxLength(10))
->add_filter('email','trim')
->add_filter('email','strtolower')
->add_rule('email',newRule\NotEmpty())
->add_rule('email',newRule\MinLength(5))
->add_rule('email',newRule\Email())
->add_rule('age',newRule\NotEmpty())
->add_rule('age',newRule\NumRange(13,18))
->add_rule('password',newRule\NotEmpty())
->add_rule('password',newRule\MinLength(5))
->add_rule('password',newRule\Matches('password2'))
->set_label('password2','password confirmation')
;
if(isset($_POST['submit'])){
if($validator->is_valid($_POST)){
echo'<p>Posted successfully.</p>';
}else{
echo'<p>Errors were encountered:</p><ul>';
foreach($validator->get_errors()as$error)echo"<li>$error</li>";
echo'</ul>';
}
}
$jquery_validator=newHybridLogic\Validation\ClientSide\jQueryValidator($validator);
$jquery=$jquery_validator->generate();
?><!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Validation Test</title>
</head>
<body>
<form action="" method="post">
<p>
<label for="name">Your name</label><br>
<input type="text" name="name">
</p>
<p>
<label for="email">Email</label><br>
<input type="text" name="email">
</p>
<p>
<label for="email">Age</label><br>
<input type="text" name="age">
</p>
<p>
<label for="password">Password</label><br>
<input type="text" name="password">
</p>
<p>
<label for="password2">Password confirmation</label><br>
<input type="text" name="password2">
</p>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.10.0/jquery.validate.js"></script>
<script type="text/javascript">
<?phpforeach($jquery['methods']as$method_name=>$method_function):?>
jQuery.validator.addMethod("<?phpecho$method_name;?>", <?phpecho$method_function;?>);
<?phpendforeach;?>
$("form").validate({
submitHandler: function(form, e) {
console.log("SUBMIT", form, e);
},
rules: <?phpechojson_encode($jquery['rules']);?>,
messages: <?phpechojson_encode($jquery['messages']);?>
});
</script>
</body>
</html>