MattSull
MattSull

Reputation: 5564

jQuery Validate plugin - displaying validation messages

i'm using the above to validate a simple contact form, based on an example found here: http://alittlecode.com/files/jQuery-Validate-Demo/

there are two rules for the control-label 'name' - 'minlength' and 'required':

$(document).ready(function(){
    $('#contact-form').validate({
    rules: {
      name: {
        minlength: 2,
        required: true
                ...

if either one of the rules are not met i can get a generic message to be displayed:

 ...
messages: {
    name: "This field is mandatory"
    }
  }); 

});

how would i go about getting a custom message for each form item's rule(s)? ie if there's 1 character entered it should display "min 2 characters needed" and if nothing's entered in a required section it should display "mandatory". i'm not too familiar with javascript - something like this? (i know it's probably wrong but it gives the idea of what i'm trying to do)

 ...
messages: {
    name(minlength): "min 2 characters needed",
    name(required): "mandatory"
    }
  }); 

});

Upvotes: 0

Views: 398

Answers (1)

PerL
PerL

Reputation: 56

$(document).ready(function () {
$('#contact-form').validate({
    rules : {
        MySuperMessage : {
            required : true,
            MySuperMessage : true
        }
    },
    messages : {
        MySuperMessage : "TEST!"
    }
});
});


<input type="text" name="MySuperMessage" id="MySuperMessage">

Upvotes: 0

Related Questions