Custom Validation Controls

Custom validation controls are a nice tool that you can use when the standard validation controls just won’t do the job.  Most examples I found out there only addressed the server side validation aspect.  Don’t get me wrong, you can do that, just use server side, but ideally you want to do both.

Trust me though, if you only want to do one side, do server side.  You will want to always do server side as a minimum just in case the user has javascript disabled or something.  Server side only is kind of old school but it gets the job done very reliably.

O.K.  On to my example.  I have a web form and lets say I have a field that I want to make sure is a Float so I can save it into my database.  It is nothing special, just a text box.  Also for this, lets pretend I already have a required field validator in place.  I always use those for fields I require, although you probably could set it to validate empty text, I just always use required field validators for required fields.

<asp:TextBox ID="txtMyFloat" runat="server" Text="" />

Next we need to create the methods to do our validation.  First the easy one, the code behind.  We are going to call this method cv_ValidFloat.

protected void cv_ValidFloat(object sender, ServerValidateEventArgs e)
{
    bool bValid = false;
    try
    {
        float fCheck = float.Parse(e.Value);
        bValid = true;
    }
    catch (Exception ex)
    {
        ex = null;
        bValid = false;
    }
    finally
    {
        e.IsValid = bValid;
    }
}

Now that we have that, lets create our client side function in javascript.

function ValidFloat(sender, args) {
    var vParse = parseFloat(args.Value);
    args.IsValid = !isNaN(vParse);
}

You see we still have our familiar sender and args variables. You can use the args just like you normally do in the code behind, mainly to get the Value and set the IsValid. So lets see what our custom validation control looks like.

<asp:CustomValidator ID="valcMyFloat" runat="server" ControlToValidate="txtMyFloat"
    Text="*" ErrorMessage="Your Float entry does not appear valid." SetFocusOnError="true"
    Display="Dynamic" ClientValidationFunction="ValidFloat" OnServerValidate="cv_ValidFloat"
    ValidateEmptyText="false" EnableClientScript="true">
</asp:CustomValidator>
Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on January 31, 2012, in .NET, C# and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: