views:

888

answers:

4

Can you tell me if there anybody has implemented a custom validator for checking that one of two (or N) input fields are filled?

   "Insert phone number or email address"

I'm using ASP.NET (Ajax) 3.5, the ajaxToolkit:ValidatorCalloutExtender (and jQuery if it's necessary).

+3  A: 
splattne
+1  A: 

I am not one to push commercial products here but when it comes to asp.net validation you would do yourself an incredible disservice not to take a look at peterblum.com. Best asp.net control package bar none...Hanselmans take on them -

"Not an add-in but rather a complete re-imagining of the ASP.NET Validation Framework. There's a learning curve, but it will change the way you write pages. Also check out his Visual Security Security and Peter'sDatePackage. His documentation is legendary."

keithwarren7
A: 

Code project also has this control (by yours truly):
http://www.codeproject.com/KB/validation/AtLeastOneOfValidator.aspx

Joel Coehoorn
A: 

I just did this (requires jQuery):

JS:

function validatePhoneOrEmail(source, args) {
    if ($("[id$='txtEmail']").val() == "" && $("[id$='txtTel']").val() == "") 
        args.IsValid = false;
    else
        args.IsValid = true;
}

ASP.NET:

<asp:CustomValidator runat="server" 
    ClientValidationFunction="validatePhoneOrEmail" 
    ErrorMessage="Please enter a telephone number or email address">
</asp:CustomValidator>

I don't have any server-side validation for this, but I'm assuming a similar function in the server would be pretty easy to create.

Ian Grainger
Obiviously you could one-line the validator if you wanted :P
Ian Grainger