Form validation your way.
In Validity, validation is an event.
So, validation code is bound to a form.
// Simply select the form in jQuery // and call the validity function on it. $("#myFormId").validity(function() { // In here, // specify how the validation is to be done. });
To insist that the input with id "user_name" has a value.
$("#myFormId").validity(function() { // Select the input, // and call a validator method on it. $("#user_name").require(); });
To insist that the input named "quantity" has a numeric value or no value at all.
$("#myFormId").validity(function() {
$("#quantity").match("number");
});
What if we want to insist that quantity has a value and that the value is numeric? We can combine these validators.
Validator methods follow the same chaining pattern as jQuery; we can just call one after another.
$("#myFormId").validity(function() { // Call the validators in sequence // starting with the most important. $("#quantity") .require() .match("number"); });
<!DOCTYPE html> <html> <head> <title>Example Time!</title> <link rel="Stylesheet" href="jquery.validity.css" /> <script src="jquery-1.6.2.min.js"></script> <script src="jquery.validity.min.js"></script> <script> $(function() { $("form").validity(function() { $("#quantity") .require() .match("number"); }); }); </script> </head> <body> <form method="get" action=""> <input type="text" id="quantity" /> <br /> <input type="submit" /> </form> </body> </html>