0
votes

I have a text box control in asp with 3 different validators. Each validator is getting its error message from the server, and each one validates different things. My problem is that for some values, two or more validators are firing and I'm getting more then one error message. I would like to make some kind of priority functionality, meaning that if the first validator is firing the other two will not. Is there any way to make the validator behave like that?

I've added some code sample:

<asp:RequiredFieldValidator ID="cvRequired" runat="server" Display="Dynamic"
    ControlToValidate="txtBox" />
<asp:RegularExpressionValidator ID="cvFormat" runat="server" Display="Dynamic"
    ControlToValidate="txtBox" ValidationExpression="^([A-Za-z])+$" />
<asp:CustomValidator ID="cvCustom" runat="server" Display="Dynamic"
    ControlToValidate="txtBox" ClientValidationFunction="validateFunction" />

I want that the format validator and the custom validator will not fire if the required validator is invalid (actually, I just want them to not showing their error message).

As I said, the error messages are from the server, so I can't really join them to one custom validator. Also, the "validateFunction" is in another js file (for re-use).

3
Can you paste some code for the issue so that we can better understand it? - Dev

3 Answers

0
votes

Few logic options you got to think about,

(txtPhone) having three validators.
1.RangeValidator, 2.CustomValidator 3.Regexvalidator

Say,after validation (check what it returns if validation fails/passes) and act upon that.

 if(rangevalidator1 != null)
{
 ...somecode...
} 

I ll suggest you using javascript ..

0
votes

you can use a single custom validator for all three validation and you put your code in if condition according to your need.

<asp:CustomValidator runat="server" ID="cstmStartDateValidater"                     
                        ToolTip="Start date cannot be greater than equal to end date/time or less than current date/time" 
                        ErrorMessage="*" ControlToValidate="txtStartDateTime"
                     ForeColor="Red" ValidationGroup="vlgMessage" SetFocusOnError="true" 
                        onservervalidate="cstmStartDateValidater_ServerValidate" ></asp:CustomValidator>

in the .cs page

protected void cstmStartDateValidater_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (CompareStartDate())
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

you can use following link for more information :

MSDN,
Code Project

hope these will help you .

0
votes

Make use of ValidatorCalloutExtender control which is available in ajax control toolkit. Place a separate ValidatorCalloutExtender across each control,you wish to validate it.