Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Fall 2019

## Form Validation

- form3.html

      $(document).ready(function(){
        var validation = $("#myForm").validate(
         {
           rules: {
             name: { required: true, minlength: 2 },
             email: { required: true, email: true },
             url: { required: true, url: true },
             favnum: { required: true, number: true },
             favnum10: { required: true, number: true, min: 1, max: 10},
             color: {required: true},
             season: {required: true, minlength: 2},
             yourdate: { required: true, date: true }
           },
           messages: {
                  season: { minlength: jQuery.validator.format("Pick a minimum of {0} choices") }
           },
           errorPlacement: function(error,element) {
               if ( element.is("[name=color]") )
                  error.appendTo( $('p#color') );
               else if ( element.is("[name=season]") )
                  error.appendTo( $('p#season') );
               else if ( element.is("[name=comments]") )
                  error.insertAfter( $('label[for=comments]') );
               else
                  error.insertAfter(element);
           }
         });
      });

Copyright © David Heitmeyer