最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery validation plugin maxlength? - Stack Overflow

programmeradmin2浏览0评论

I am using JQuery validation plugin. I want to specify maxlength for one of the fields.It can be specified as below.

rules: {
   Message: {
      required: false,
      maxLength: 200
   }
}

But instead of defining the rules externally, i want to specify the rules inside html input code

Similar to :

<input type="text" name="AssistanPhone" value="" class="required"  />

In above example "required" is specified through class. Similarly how can i specifiy maxlength which can be recognized by jquery plugin and gives error message if length exceeds?

Thanks!

I am using JQuery validation plugin. I want to specify maxlength for one of the fields.It can be specified as below.

rules: {
   Message: {
      required: false,
      maxLength: 200
   }
}

But instead of defining the rules externally, i want to specify the rules inside html input code

Similar to :

<input type="text" name="AssistanPhone" value="" class="required"  />

In above example "required" is specified through class. Similarly how can i specifiy maxlength which can be recognized by jquery plugin and gives error message if length exceeds?

Thanks!

Share Improve this question edited Aug 22, 2013 at 7:15 Arun P Johny 388k68 gold badges531 silver badges532 bronze badges asked Aug 22, 2013 at 7:09 user755806user755806 6,81527 gold badges111 silver badges157 bronze badges 1
  • What does this have to do with Java? – nanofarad Commented Aug 22, 2013 at 7:10
Add a comment  | 

4 Answers 4

Reset to default 7

Not capital L its l

Ex:

$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      maxlength: 200
    }
  }
});

PLUGIN DEMO

As far as I can see you cannot specify the messages via attributes, but the maxlength can be specified as a attribute

<input type="text" name="AssistanPhone" value="" required maxlength="3"  />

Demo: Fiddle

I updated the javascript code of the validator myself (so I'm stuck with my version 1.8.1 now instead of upgrading), but here's what I did (around line 767):

classRules: function(element) {
    var rules = {};
    var classes = $(element).attr('class');
    classes && $.each(classes.split(' '), function() {
        if (this in $.validator.classRuleSettings) {
            $.extend(rules, $.validator.classRuleSettings[this]);
        }
        if (this.toLowerCase().lastIndexOf('maxlength-', 0) === 0) { // starts with
            var x = parseInt(this.substring(10)); // take number after 'maxlength-'
            $.extend(rules, {maxlength: x});
        }
    });
    return rules;
},

I added the extra if-test for "maxlength-", so now I can add a class like "maxlength-10" to limit to 10. Of course I could also add minlength and so on.


  $("#FormID").validate({
        rules: {
            PriorityDDL: {
                required: true
            },
            Title: {
                required: true
            },
            Text: {
                required: true,
                maxlength: 300
            },

            date: {
                required: true
            },
            reportfile: {
                required: true
            }
        },
        messages: {
            PriorityDDL: {
                required: "Please select priority"
            },
            Title: {
                required: "Please enter title"
            },
            Text: {
                required: "Please enter message",
                maxlength:"maxLength is 300 characters"
            },
            date: {
                required: "Please select date"
            },
            reportfile: {
                required: "Please select file"
            }
        }
    });

发布评论

评论列表(0)

  1. 暂无评论