Monday, May 6, 2013

How to validate multiple form fields with jQuery validation plugin

jQuery validation plugin is a helpful and easy to use validation tool to validate form.
From the examples shown in the jQuery validation plugin official site, we can see the validation on single form field only.
Credit card validation and Email validation with jQuery in my blog are the single form field validation example.
We can actually validate multiple form fields in one shot with jQuery.

Below is the sample syntax to validate multiple form fields with jQuery validation plugin.
COMMA(,) is the separator of multiple form fields.
<script>
$(document).ready(function(){
  $("#myform").validate({
    rules: {
      field1: {
        email: true
      },
      field2: {
        required: true
      },
      fieldN: {
        creditcard: true
      }
    }
  });
});
</script>

so now let's combine both the Credit card validation and Email validation sample codes into 1.
<script>
$(document).ready(function(){
  $("#myform").validate({
    rules: {
      ccard: {
        required: true,
        creditcard: true
      },
      emailAddr: {
        required: true,
        email: true
      }
    }
  });
});
</script>



Done!!


LinkWithin

Related Posts Plugin for WordPress, Blogger...