UNPKG

formvalidation

Version:

The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks

101 lines (93 loc) 3.77 kB
<!DOCTYPE html> <html> <head> <title>FormValidation demo</title> <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/> <link rel="stylesheet" href="../dist/css/formValidation.css"/> <script type="text/javascript" src="../vendor/jquery/jquery.min.js"></script> <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="../dist/js/formValidation.js"></script> <script type="text/javascript" src="../dist/js/framework/bootstrap.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-8 col-lg-offset-2"> <div class="page-header"> <h2>Using events</h2> <p class="lead">The captcha is regenerated if the input is not valid</p> </div> <form id="form" method="post" class="form-horizontal" action="target.php"> <div class="form-group"> <label class="col-lg-3 control-label">Full name</label> <div class="col-lg-5"> <input type="text" class="form-control" name="fullName" /> </div> </div> <div class="form-group"> <label class="col-lg-3 control-label" id="captchaOperation"></label> <div class="col-lg-2"> <input type="text" class="form-control" name="captcha" /> </div> </div> <div class="form-group"> <div class="col-lg-9 col-lg-offset-3"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> </div> </div> </div> <script type="text/javascript"> $(document).ready(function() { // Generate a simple captcha function randomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }; function generateCaptcha() { $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' ')); }; generateCaptcha(); $('#form') .formValidation({ message: 'This value is not valid', icon: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { fullName: { validators: { notEmpty: { message: 'The full name is required' } } }, captcha: { validators: { callback: { message: 'Wrong answer', callback: function(value, validator, $field) { var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]); return value == sum; } } } } } }) .on('err.form.fv', function(e) { var $form = $(e.target), formValidation = $form.data('formValidation'); if (!formValidation.isValidField('captcha')) { // The captcha is not valid // Regenerate the captcha generateCaptcha(); } }); }); </script> </body> </html>