我们可以使用jquery进行自定义验证控件的客户端验证吗?

时间:2021-12-26 00:10:57

There are two textboxes one for email and other one for phone i have used one custom validation control so that user have to fill any one of textboxes for client side i used javascript

有两个文本框一个用于电子邮件,另一个用于手机我使用了一个自定义验证控件,以便用户必须填写任何一个文本框的客户端我使用的JavaScript

function ValidatePhoneEmail(source, args) {

function ValidatePhoneEmail(source,args){

        var tboxEmail = document.getElementById('<%= tboxEmail.ClientID %>');
        var tboxPhone = document.getElementById('<%= tboxPhone.ClientID %>');
        if (tboxEmail.value.trim() != '' || tboxPhone.value.trim() != '') {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }

how to achieve same result using jquery

如何使用jquery实现相同的结果

2 个解决方案

#1


2  

The exact validation plugin equivalent would be to use "required" on those fields, like this:

确切的验证插件等价物将在这些字段上使用“required”,如下所示:

$(function() {
  $("form").validate({
    rules: {
      <%= tboxEmail.UniqueID %>: "required",
      <%= tboxPhone.UniqueID %>: "required"
    }
  });
});

You can see a full list of options here. If you wanted to add a custom message and validate that it is a valid email address, you can do it like this:

您可以在此处查看完整的选项列表。如果您想添加自定义消息并验证它是有效的电子邮件地址,您可以这样做:

$(function() {
  $("form").validate({
    rules: {
      <%= tboxEmail.UniqueID %>: { required: true, email: true },
      <%= tboxPhone.UniqueID %>: "required"
    },
    messages: {
      <%= tboxEmail.UniqueID %>: "Please enter a valid email address",
      <%= tboxPhone.UniqueID %>: "Please enter a phone number"
    }
  });
});

#2


0  

  <script type="text/javascript"> 
        $(document).ready(function() { 
            $("#aspnetForm").validate({ 
                rules: { 
                    <%=txtName.UniqueID %>: { 
                        minlength: 2, 
                        required: true 
                    }, 
                     <%=txtEmail.UniqueID %>: {                         
                        required: true, 
                        email:true 
                    } 
                }, messages: { 
                    <%=txtName.UniqueID %>:{  
                        required: "* Required Field *",  
                        minlength: "* Please enter atleast 2 characters *"  
                    } 
                } 
            }); 
        }); 
    </script> 

    Name: <asp:TextBox ID="txtName"   runat="server" /><br /> 
    Email: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br /> 

#1


2  

The exact validation plugin equivalent would be to use "required" on those fields, like this:

确切的验证插件等价物将在这些字段上使用“required”,如下所示:

$(function() {
  $("form").validate({
    rules: {
      <%= tboxEmail.UniqueID %>: "required",
      <%= tboxPhone.UniqueID %>: "required"
    }
  });
});

You can see a full list of options here. If you wanted to add a custom message and validate that it is a valid email address, you can do it like this:

您可以在此处查看完整的选项列表。如果您想添加自定义消息并验证它是有效的电子邮件地址,您可以这样做:

$(function() {
  $("form").validate({
    rules: {
      <%= tboxEmail.UniqueID %>: { required: true, email: true },
      <%= tboxPhone.UniqueID %>: "required"
    },
    messages: {
      <%= tboxEmail.UniqueID %>: "Please enter a valid email address",
      <%= tboxPhone.UniqueID %>: "Please enter a phone number"
    }
  });
});

#2


0  

  <script type="text/javascript"> 
        $(document).ready(function() { 
            $("#aspnetForm").validate({ 
                rules: { 
                    <%=txtName.UniqueID %>: { 
                        minlength: 2, 
                        required: true 
                    }, 
                     <%=txtEmail.UniqueID %>: {                         
                        required: true, 
                        email:true 
                    } 
                }, messages: { 
                    <%=txtName.UniqueID %>:{  
                        required: "* Required Field *",  
                        minlength: "* Please enter atleast 2 characters *"  
                    } 
                } 
            }); 
        }); 
    </script> 

    Name: <asp:TextBox ID="txtName"   runat="server" /><br /> 
    Email: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />