开发者

can we use jquery for client side validation of a custom validation control?

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

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


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

$(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"
    }
  });
});


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜