Form validaion is a bit like picking spuds, you know you want to eat the spuds, but you got to dig them and then pick them (spuds are potatoes for the non Irish reading this) so how do we do this efficiently, well I’ve been starting to use the jQuery Validation Plugin it seems to work pertty good, and it’s plain and simple, so how does it work, well the doc’s are a bit sparce, and it’s javascript, which isint the easyest language in the world to program in.
so here goes:
Put below at the top of your html web page, this collects the jquery and the jquery validate library’s
1 |
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js"></script> |
1 |
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> |
1 |
<script src=http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> |
1 |
<script src=http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script> |
1 |
<script src=http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> |
Then you need add the scripting part, i do a standard debug check to see that jquery works first.
1 |
<script> |
1 |
$(document).ready(function () { |
1 |
if (typeof jQuery != 'undefined') { |
1 |
//jQuery is loaded => print the version |
1 |
alert(jQuery.fn.jquery); |
1 |
} |
1 |
1 |
1 |
$('#Process').validate({ // initialize the plugin |
1 |
rules: { //Now we set up the rules |
1 |
email: { // this one is for e-mail, its going to check the e-mail is correct |
1 |
required: true, |
1 |
email: true |
1 |
}, |
1 |
1 |
} |
1 2 3 |
}); //End of Process Validate }); // end of document ready function |
1 |
</script> |
Now we have the script ended we can start the HTML and the form
1 2 |
<h1>Booking Form</h1> <FORM action="process.php" method="POST" id="Process"> |
1 |
email <br> |
1 |
<INPUT type="text" name="email"> |
1 2 |
<INPUT type="submit" value="Confirm"> </FORM> |
Thats pretty much it, yes you can do alot more fancy stuff, but this is a start to get you going.