简体   繁体   English

要在jQuery验证引擎中使用自定义规则?

[英]To use of custom rules in jQuery Validation Engine?

I use jQuery Validation Engine for my form validation here is link for demo : https://github.com/posabsolute/jQuery-Validation-Engine 我使用jQuery Validation Engine进行表单验证,这里是演示链接: https : //github.com/posabsolute/jQuery-Validation-Engine

I have one checkbox and four textboxes in ASP.NET, I want to validate that if the checkbox is selected then the four textboxes must be filled or it would show an error message that "please fill the boxes." 我在ASP.NET中有一个复选框和四个文本框,我想验证是否选中了该复选框,则必须填充四个文本框,否则它将显示一条错误消息“请填充这些框”。

This is so far I have tried, but didn't work. 到目前为止,我已经尝试过了,但是没有用。 Any help would be appreciated. 任何帮助,将不胜感激。

  <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>

Looks like your problem is with the fact that asp.net control IDs are generated on the client side, so you cannot really reference them as is in your script. 看起来您的问题在于,asp.net控件ID是在客户端生成的,因此您不能像在脚本中那样真正引用它们。 They are available though via ClientID, and since your script appears to be defined on the aspx page and not in a separate js file, the easiest thing for you to do is this: 它们可以通过ClientID获得,并且由于您的脚本似乎是在aspx页面上定义的,而不是在单独的js文件中定义的,因此您最容易做到的是:

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

Likewise for the textboxes. 对于文本框也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM