Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire only one Custom Validator at a time

Tags:

c#

asp.net

I have two Custom Validators attached to a textbox. The textbox is to enter comma separated list of emails. The first validator checks that there is not duplicate email address. The second custom validator checks that all the email addresses are in valid format. I have following code and it works fine.

However, when both the failure criteria is met both of them are firing and I get two * at the right side of my textbox. What is the best approach to show only one * at a time?

enter image description here

CODE

<asp:CustomValidator ID="valEmailRecipients" runat="server" ControlToValidate="txtEmailRecipients"
ClientValidationFunction="isUniqueElements" OnServerValidate="IsUniqueEmail_Validate"
Text="*" ErrorMessage="There should be no duplicate email address for a report"
ValidationGroup="Save">
</asp:CustomValidator>

<asp:CustomValidator ID="valEmailFormat" runat="server" ControlToValidate="txtEmailRecipients"
ClientValidationFunction="checkEmailFormat" OnServerValidate="IsValidEmailFormat_Validate"
Text="*" ErrorMessage="Entries should be valid email addresses separated by comma"
ValidationGroup="Save">
</asp:CustomValidator>

REFERENCES:

  1. Validating with a Custom Function for ASP.NET Server Controls
  2. Enable/Disable asp validation control in javascript
  3. How to set ValidatorEnable() javascript attribute for a validator?
  4. Validator enable using Javascript
like image 368
LCJ Avatar asked Mar 11 '26 18:03

LCJ


2 Answers

Easiest way:- Have the valEmailRecipients return true if the email is not in a correct format. That is it will do it's function only if other validation was successful.

Other way:- Disabling and Enabling validators, which can be a pain.

Just out of curiosity, why a problem with 2 asterisks?

like image 84
Brian Mains Avatar answered Mar 14 '26 08:03

Brian Mains


Create a single Custom Validator, and perform all the validation logic on it.

If one validation didn't pass, set the according error message to it, set the args.IsValid to false and end the method.

On the submit button, remember to check Page.IsValid.

like image 36
Marlon Vidal Avatar answered Mar 14 '26 08:03

Marlon Vidal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!