Is it possible to assign two validators for a text box?I had used a regularexpression validator and a requiredfieldvalidator for a textbox txtAccount,but while execution only regularexpressionvalidator is working.I used this code:
<asp:RegularExpressionValidator ID="REV1" runat="server"
ErrorMessage="should be a numeric value"
ValidationExpression="\d*" ControlToValidate="txtAccount">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RFV1" runat="server"
ErrorMessage="AC.No already exists" ControlToValidate="txtAccount">
</asp:RequiredFieldValidator>
Yes, you can assign two validators to the same control; your problem is that your Validators are sort of "mutually exclusive" and you think that only one is firing.
You have a regular expression validator that's supposed to make sure that you only enter digits and you have a required field validator that's supposed to make sure that you enter "something"; hence, if you enter "something", the regular expression validator will kick in if the "something" entered is not a number. And if you don't enter anything, your required field validator will kick in and tell you that you need to "enter something" but you won't see the regular expression validator telling you that the input needs to be a number when you haven't even entered anything yet! Do you follow?
Perhaps you want to take a look at the ValidationSummary control.
Icarus is right. if you are entering something in the text box, it satisfies the condition of required field validator. So you only see is regular expression field validator is working. Require field validator just checks that your text box is not left blank. if your want custom logic for your validation kindly use custom validator. Refer this link for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With