Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validation in asp.net

Tags:

asp.net

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>   
like image 232
diya Avatar asked Oct 10 '22 00:10

diya


2 Answers

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.

like image 131
Icarus Avatar answered Oct 12 '22 13:10

Icarus


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

like image 37
Rishikesh Vaishy Avatar answered Oct 12 '22 15:10

Rishikesh Vaishy