【问题标题】:To use of custom rules in jQuery Validation Engine?在 jQuery 验证引擎中使用自定义规则?
【发布时间】:2016-07-30 17:27:25
【问题描述】:

我使用 jQuery Validation Engine 进行表单验证,这里是演示链接: https://github.com/posabsolute/jQuery-Validation-Engine

我在 ASP.NET 中有一个复选框和四个文本框,我想验证如果选中该复选框,则必须填写四个文本框,否则会显示“请填写这些框”的错误消息。

到目前为止,我已经尝试过,但没有奏效。 任何帮助将不胜感激。

  <script type="text/javascript">
                function checkBox() {
                    var check = document.getElementById("CheckBox1");
                    var tb1 = document.getelementbyid('iBox1');
                    var tb2 = document.getelementbyid('iBox2');
                    var tb3 = document.getelementbyid('iBox3');
                    var tb4 = document.getelementbyid('iBox4');
         if (check.checked && tb1 == null && tb2 == null && tb3 == null && tb4 == null) {
                        alert("checkbox checked");
                        jQuery("#form1").validationEngine({
                            'custom_error_messages': {
                                '#iBox1': {
                                    'required': {
                                        'message': "write at least four characters in Name"
                                    }
                                }
                            }
                        });
                    }
                    else {
                        alert("unchecked");
                    }
                }
                $(document).ready(function () {
                    $("#form1").validationEngine();
                });
        </script>
        <form runat="server" id="form1">
        <div>
       <asp:CheckBox runat="server" ID="CheckBox1" onclick="checkBox()"></asp:CheckBox>
        <asp:TextBox runat="server" ID="iBox1"  MaxLength="1" size="1">/asp:TextBox>
        <asp:TextBox runat="server" ID="iBox2" MaxLength="1" size="1"></asp:TextBox>
        <asp:TextBox runat="server" ID="iBox3" MaxLength="1" size="1"></asp:TextBox>
        <asp:TextBox runat="server" ID="iBox4" MaxLength="1" size="1"></asp:TextBox>
        </div>
    </form>

【问题讨论】:

    标签: jquery asp.net validation jquery-validation-engine


    【解决方案1】:

    看起来您的问题在于 asp.net 控件 ID 是在客户端生成的,因此您无法像在脚本中那样真正引用它们。它们可以通过 ClientID 获得,并且由于您的脚本似乎是在 aspx 页面上定义的,而不是在单独的 js 文件中,因此您最简单的做法是:

    var check = document.getElementById('<%= CheckBox1.ClientID %>');
    

    对于文本框也是如此。

    【讨论】:

    • 它没有触发或者点击时什么也没发生
    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 2013-03-02
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2014-04-22
    • 2016-10-04
    • 1970-01-01
    相关资源
    最近更新 更多