ijb109
ijb109

Reputation: 972

ASP.NET Custom Validator not firing

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

Upvotes: 0

Views: 9682

Answers (4)

Auri Rahimzadeh
Auri Rahimzadeh

Reputation: 2263

I don't know if it's related, but I had an issue with a custom validator not firing. I had it validating a dropdownlist, which had a default value of an empty string. I had to set

ValidateEmptyString="true" 

and then it started working just fine.

You could also call the Validate() function and read the IsValid property from the custom validator control. However, the Page's validation function should work just fine, and shouldn't require such a call.

Upvotes: 2

freefaller
freefaller

Reputation: 19953

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.

Upvotes: 2

SeaSharp
SeaSharp

Reputation: 186

On the front-end try setting the CausesValidation attribute to true as shown below. See if that helps.

<asp:Button ID="btnSubmit" Text="Submit" CausesValidation="true" runat="server" />

Upvotes: 0

dittu
dittu

Reputation: 78

I suppose you have a server side event to execute if the validation passes. for ex: a button click event. In that if you check Page.IsValid then it will be false if the validation passes then Page.IsValid will be true. You need to use Page.IsValid to check whether the validation passed or not.

Upvotes: 0

Related Questions