开发者

jQuery - Add Validator Rule Dynamically?

I'm trying to add a jQuery validator rule dynamically and it's not working. It doesn't throw any errors but doesn't trigger the validation.

Here's t开发者_运维百科he js:

$(document).ready(function () {

    $.validator.addMethod("TIME", function (value, element) {
        var re = new RegExp(regexp);
        console.debug("Validating " + value);
        return this.optional(element) || re.test("^*(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M)*$").test(value);
    }, "Time is invalid: Please enter a valid time.");

$("input[id *= '_timepicker']").rules("add", "TIME");

});

Any idea what's wrong?

EDIT:

Now I get an error: invalid quantifier *(1[0-2]|[1-9]):[0-5][0-9] (a|p|A|P)(m|M)$

Here's the relevant js:

rules:{"special.Hours.FridayHours1.close":{ regExp: /^*(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M)*$/ }

Here's the complete validate function:

http://pastebin.com/YRday2Mv


Your syntax is off just a bit, you need to pass it an object instead of a string, like this:

$("input[id*='_timepicker']").rules("add", {"TIME": true });

You can find the .rules() documentation with more examples here.


$(function() {
      $('#order_form_id').validate();
      $("input[id*='_quantity']").each(function () {$(this).rules("add", {required:true,zznum:true});})
    });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜