Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

jQuery makes it pretty easy to create form validation. All it takes it first setting the form up to validate when it submits.

<script type="text/javascript">
     $(document).ready(function() {
          $('#myForm').validate();
     });
</script>

Then you indicate the type of validations that should occur. There are a couple of ways to do this. The easiest for me is to add the validations as if they are CSS classes for the individual fields. In this example, I indicate that I want the field to be required and the value should be a valid date. Some of the other validations that are available are email, minlength, maxlength, min, max. The entire list is available here

<form name="myForm" id="myForm" action="">
        <input class="required date" type="text" id="myId" name="myDate" size="10" value="${yesterday}">
</form>

Error display is handled for you automatically. Standard validation messages are already set-up and the messages appear in red to the right of any invalid field.

By default, the validation is not triggered until the field is submitted, but any fields flagged by invalid are re-checked when they are changed.

December 15th, 2010

Posted In: javascript, JQuery

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com