tags:

views:

55

answers:

1

I am using $.validator.addMethod

How can I print the validation message in a control. I have a div id="err" where I want to print the message

Here is what my method looks like

$.validator.addMethod('something', function(value, element) {
            return false;
}, 'I want to display this message in a Div with ID=error')
+2  A: 

From the jQuery Documentation (see the options tab):

Displays a message above the form, indicating how many fields are invalid when the user tries to submit an invalid form.

$("#form").validate({
    invalidHandler: function(form, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = errors == 1
          ? 'You missed 1 field. It has been highlighted'
          : 'You missed ' + errors + ' fields. They have been highlighted';
        $("div.error span").html(message);
        $("div.error").show();
      } else {
        $("div.error").hide();
      }
    }
 })

UPDATE:

To include your custom validation method, just include it in your rules:

$("#form").validate({
  rules: {
    name: {
      MustBeAwesome: true
    }
  }
});

Validation method:

$.validator.addMethod('MustBeAwesome', function(value, element) {
            return false;
}, 'Your name is not awesome');
Eric Lennartsson
i updated my question to tell exactly what i want
ScG
Updated my answer to include what I think you're looking for.
Eric Lennartsson
It is the selector to the form which will be validated. I'll update my post to use $("#form") instead to be more clear.
Eric Lennartsson