Validation controls in ASP.NET are great tools that reduce the time needed to do such a repetitive task> However, sometimes they don't provide all what we need. We still want to use them but we also want to add more validation that is custom to our business.
Custom Validation controls come to solve this problem. We can use them in conjunction with the normal Validation controls. We can provide either our custom client-side validation or server-side validation, or both.
In many times, we use custom validation controls to validate the entire logic of a form instead of a specific control. For this reason, we can leave the
Here's a simple example of using custom validation controls. Suppose we have the following is the HTML:
And suppose that you want to force the user to enter either mobile number or home number, so you add the following custom control.
In
Similarly, we use
That's it!
Custom Validation controls come to solve this problem. We can use them in conjunction with the normal Validation controls. We can provide either our custom client-side validation or server-side validation, or both.
In many times, we use custom validation controls to validate the entire logic of a form instead of a specific control. For this reason, we can leave the
ControlToValidate
property empty.Here's a simple example of using custom validation controls. Suppose we have the following is the HTML:
<asp:Literal ID="lblMobileNumber" runat="server" Text="Mobile Number" /> <asp:TextBox ID="txtMobileNumber" runat="server" /> <asp:Literal ID="lblHomeNumber" runat="server" Text="Home Number" /> <asp:TextBox ID="txtHomeNumber" runat="server" /> <asp:Button runat="server" ID="btnSave" Text="Save" ValidationGroup="Save" />
And suppose that you want to force the user to enter either mobile number or home number, so you add the following custom control.
<asp:CustomValidator ID="cvMobileOrHomeNumber" runat="server" ErrorMessage="Please enter either mobile number or home number" ValidationGroup="Save" ClientValidationFunction="validateMobileOrHomeNumber" OnServerValidate="cvMobileOrHomeNumber_ServerValidate" />
In
ClientValidationFunction
we specify the Javascript function to be executed for validation. This function should accept 2 parameters. The first parameter represents the source of the validation, and the second parameter is the one that we'll use to eventually decide whether the operation is valid or not. Let's create the function.
<script type="text/javascript"> function validateMobileOrHomeNumber(source, args) { if (the operation is valild) { args.IsValid = true; } else { args.IsValid = false; } return; } </script>
Similarly, we use
OnServerValidate
to specify the method to be executed at server side. Let's see how to implement it.
protected void cvMobileOrHomeNumber_ServerValidate(object source, ServerValidateEventArgs args) { if (the operation is valid) args.IsValid = true; else args.IsValid = false; }
That's it!
Vice informative article regarding validation controls. Developers can also check the detailed article on this topic at http://www.webcodeexpert.com/2013/06/how-to-use-customvalidator-validation.html
ReplyDeleteReally Nice info..Thanks
ReplyDeleteI found some good stuff as yours on the following link
http://freefeast.info/general-it-articles/dotnet4-0-general-it-articles/how-to-create-custom-validation-control-in-asp-net-4-0-make-custom-validation-control-using-visual-studio/
hii.. i am new in dotnet circle .in dotnet am used only mobile number check process .now i got new idea in this blog ,
ReplyDeletethank u so much for this nice info..
dot net training in velachery |
dot net training in chennai
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleterpa Training in tambaram
blueprism Training in tambaram
automation anywhere training in tambaram
iot Training in tambaram
rpa training in sholinganallur
blue prism training in sholinganallur
automation anywhere training in sholinganallur
iot training in sholinganallur
ReplyDeleteGreat thoughts you got there, believe I may possibly try just some of it throughout my daily life.
rpa Training in Chennai
rpa Training in bangalore
rpa Training in pune
blueprism Training in Chennai
blueprism Training in bangalore
blueprism Training in pune
iot-training-in-chennai
thanks for sharing this information
ReplyDeletetableau training in BTM
tableau training in bangalore
tableau training in bangalore jayanagar
tableau training in bangalore btm
best tableau training institutes in bangalore
tableau classroom training in bangalore
Blue Prism Training in Bangalore
Blue Prism Training in BTM