Pages

27.3.12

Custom Validation Controls in ASP.NET

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

6 comments:

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

    ReplyDelete
  2. Really Nice info..Thanks

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

    ReplyDelete
  3. hii.. i am new in dotnet circle .in dotnet am used only mobile number check process .now i got new idea in this blog ,
    thank u so much for this nice info..
    dot net training in velachery |
    dot net training in chennai

    ReplyDelete
  4. 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.

    rpa 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

    ReplyDelete

Promotional Code for Udemy ServiceNow CIS - HR Practice Tests

If you're planning to become ServiceNow Certified Implementation Specialist - Human Resources (CIS-HR), you can prepare for the exam usi...