Multiple ASP .Net Validators on the same textbox

When  I use asp:RequiredFieldValidator together with asp:RegularExpressionValidator as below on the same textbox, only the asp:RegularExpressionValidator  fires and asp:RequiredFieldValidator is never used. If I use the asp:RequiredFieldValidator alone and remove the other one, it alone works.

<asp:RegularExpressionValidator runat="server" ControlToValidate="txtLottorekke" ValidationExpression="\d{1,2},\d{1,2},\d{1,2},\d{1,2},\d{1,2},\d{1,2},\d{1,2}" Display="Dynamic" ValidationGroup="personInputGrp">
	<br/><span class="alert alert-danger pull-left customErrorStyle" role="alert" style="">
		<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"/>
		Correct format is d,d,d,d,d,d,d
	</span>
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator Display="Dynamic" runat="server" ControlToValidate="txtLottorekke" style="color:red;" ValidationGroup="personInputGrp">
	<br/><span class="alert alert-danger pull-left customErrorStyle" role="alert">
		<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"/>
		Field is required
	</span>
</asp:RequiredFieldValidator>

Open in new window


If I add a custom validator in front of the two, then nothing works. When I put a break in the server side code, I see that it is never reached when I click the submit button:
<asp:CustomValidator id="txtLottoCustomValidator" ControlToValidate="txtLottorekke" OnServerValidate="CustomLotteryNumberValidation" ValidationGroup="personInputGrp" Display="Static" runat="server">
	<br/><span class="alert alert-danger pull-left customErrorStyle" role="alert" style="">
		<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"/>
		Lottery number is not correct
	</span>
</asp:CustomValidator>

Open in new window

Server side:
    protected void CustomLotteryNumberValidation(object source, ServerValidateEventArgs arguments) {
       bool isValid = true;
       foreach(string strNumber in  arguments.Value.Split(',')) {
          int number = Int32.MaxValue;
          Int32.TryParse(strNumber, out number);
          if(number < 1 || number > 34) {
            isValid = false;
            break;
          }

       }
       arguments.IsValid = isValid;
    }

Open in new window

And here is my simple submit button:
<asp:Button ID="btnSubmitNotes" runat="server" Text="Submit" class="btn btn-primary" ValidationGroup="personInputGrp" OnClick="btnSubmitNotes_Click" />

Open in new window


How can I get them all to work nicely together?
LVL 2
itniflAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CamilliaCommented:
I copied your customvalidator code in a new project/page I created.  I set the debug and it does stop in the CustomLotteryNumberValidation method.

Try your customvalidator code in a separate project and see if it works.
itniflAuthor Commented:
If I change the custom valiator to only the following, then the custom validator also works. But the second validator (:RequiredFieldValidator) isn't "firing."

<asp:CustomValidator id="txtLottoCustomValidator" ControlToValidate="txtLottorekke" OnServerValidate="CustomLotteryNumberValidation" ErrorMessage="Wrong format" Display="Dynamic" runat="server"/>

Open in new window


This is the same problem as described in the beginning of the question. When using both RegularExpressionValidator (first) and RequiredFieldValidator (second), the last validator never gets used.

I want to b able to use several validators on the same text box, and have them all check for validity and report status.
CamilliaCommented:
I think only the first validator is fired because that condition is met first. Condition is met and error msg is displayed.

Your custom validator gets fired. So, why don't put all your validation in that method?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

itniflAuthor Commented:
I coul do that, but from what I can find on Google it looks like multiple validators on one control should work. But can't confirm it with any Microsoft source.
itniflAuthor Commented:
Thanks, I think it should work but I guess it does not.
itniflAuthor Commented:
Regarding my validators, I now from a collegue found the solution.
This was added to Global.asax.cs:
protected void Application_Start(object sender, EventArgs e)
{
ScriptResourceDefinition myScriptResDef = new ScriptResourceDefinition();
myScriptResDef.Path = "~/Scripts/jquery-1.4.2.min.js";
myScriptResDef.DebugPath = "~/Scripts/jquery-1.4.2.js";
myScriptResDef.CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js";
myScriptResDef.CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js";
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", null, myScriptResDef);
}

Open in new window

This was added to the web.config:

<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

Open in new window


Also, I can't use a  <span/. It has to be opened <span> and closed </span>. That is why text was not being shown later down in the table cell. Ended up like this:

<asp:RequiredFieldValidator ID="val4" runat="server" Display="Dynamic" ValidateRequestMode="Enabled" ErrorMessage="Feil" ForeColor="Red" ControlToValidate="txtName">
<br/><span class="alert alert-danger pull-left customErrorStyle" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true">
Required field
</span>
</span>
</asp:RequiredFieldValidator>

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.