【问题标题】:How to dynamically add a validation rule using jquery validation plugin如何使用 jquery 验证插件动态添加验证规则
【发布时间】:2016-02-07 13:26:04
【问题描述】:

我正在尝试使用http://jqueryvalidation.org/ 向某些动态控件动态添加验证规则,但它不起作用。

 $(".rtxt").each(function () {
             $(this).rules('add', {
                 required: true
             });
         });

 <input class='rtxt'  name='txtDocName' id='txtDocName' style='width:220px;' type='text' >

不知道我在这里缺少什么。

我不能使用 'required' 属性,因为 IE9 不支持它,所以我必须使用 jquery 验证插件。

这是小提琴

http://jsfiddle.net/ed4fg1xo/

另外,我需要对 div 点击事件进行验证。

【问题讨论】:

    标签: javascript jquery validation jquery-plugins


    【解决方案1】:
    $(document).ready(function () {
        // 1. prepare the validation rules and messages.
        var rules = {
            textbox1: {
                required: true,
                minlength: 2
            },
            textbox2: "required",
            textbox3: "required"
        };
        var messages = {
            textbox1: {
                required: "textbox1 is required",
                minlength: "textbox1 needs to be at least length 2"
            },
            textbox2: "textbox2 is requried",
            textbox3: "textbox3 is required"
        };
    
        // 2. Initiate the validator
        var validator
            = new jQueryValidatorWrapper("FormToValidate",
                rules, messages);
    
        // 3. Set the click event to do the validation
        $("#DivIdName").click(function () {
            if (!validator.validate())
                return;
    
            alert("Validation Success!");
        });
    });
    

    【讨论】:

    • 我需要在哪里放置 $("#yourformname").validate();我需要验证 div 点击事件的控件。
    • 查看此链接:forums.asp.net/t/…
    • 我需要放置 $("#yourformname").validate();在 div 点击事件中?
    • 我在 var validator = new jQueryValidatorWrapper("form1", rules, messages) 上收到错误消息“Microsoft JScript 运行时错误:'jQueryValidatorWrapper' 未定义”;
    • 在 jquery.validate 插件之前加载 jquery-1.7.1.min.js。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多