C# – ASP.NET Custom Validator not firing

asp.netcvalidation

Here's the deal: I built a custom validator that should only fire if there is input in the text box. When it fires, it should test to see if the text in the box is an integer, and it will reject it if not. It won't fire, though, and I'm trying to figure out why.

I put this void in the body of the partial c# class:

    protected void intValidate_Validate(object sender, ServerValidateEventArgs arg)
    {
        int num;
        bool isNum = int.TryParse(arg.ToString(), out num);

        if(arg.Value.Length>0){
            if (isNum)
             {
                arg.IsValid = true;
             }
             else
             {
                arg.IsValid = false;
             }
        }
        else{
            arg.IsValid=true;
        }
    }

The code for the validator is as follows:

 <div class="adult">
            <label>Adults ($10)</label>
            <asp:TextBox runat="server" ID="wAdultLunch" class="adultLunch" MaxLength="2" />
            <asp:CustomValidator ID="intValidate" ControlToValidate="wAdultLunch" ErrorMessage="Invalid number" OnServerValidate="intValidate_Validate" Display="Static" runat="server" EnableClientScript="False" ValidateEmptyText="True"></asp:CustomValidator>
 </div>

Insight would be appreciated!

EDIT: I attached the postback code below

<asp:Button ID="wSubmit" runat="server" Text="Submit" OnClientClick="return validateForm();" causesvalidation="true"/>

Best Answer

You are doing the parse on the wrong thing. arg.ToString() will give you the string of "System.Web.UI.WebControls.ServerValidateEventArgs", when you actually want to the arg.Value (which is already a string)

So, instead of...

bool isNum = int.TryParse(arg.ToString(), out num);

Change it to (note the .Value)...

bool isNum = int.TryParse(arg.Value, out num);

Although it is a lot more complex than it actually needs to be. The whole thing could be rewritten a lot more comprehensively like this...

protected void intValidate_Validate(object sender, ServerValidateEventArgs arg)
{
    int num;
    arg.IsValid = int.TryParse(are.Value, out num);
}

This is because TryParse will return a true if the conversion was successful, and false otherwise.

And as a final thought, all this could be achieved using the <asp:RequiredFieldValidator ...>, <asp:CompareValidator Operator="DataTypeCheck" Type="Integer" ...> and <asp:RangeValidator ...> validator controls.


UPDATE

As the OP points out, he doesn't want the error when when the textbox is empty, so instead try this...

protected void intValidate_Validate(object sender, ServerValidateEventArgs arg)
{
    if (args.Value.Length > 0)
    {
        int num;
        arg.IsValid = int.TryParse(are.Value, out num);
    }
    else
    {
        arg.IsValid = true;
    }
}

And has already been pointed out by @HansKesting... this will only ever be called if all client-side validation is passed. As soon as client-side validation fails, the submission of the form back to the server is also cancelled.

Related Topic