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')

1 Answer 1

2

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Updated my answer to include what I think you're looking for.
It is the selector to the form which will be validated. I'll update my post to use $("#form") instead to be more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.