开发者

Using the jQuery Validator

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')
开发者_开发技巧


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜