Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Custom client-side validator for "one of two fields must be filled"? [closed]

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).

like image 644
splattne Avatar asked Dec 10 '22 22:12

splattne


1 Answers

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.

like image 148
Ian Grainger Avatar answered May 12 '23 08:05

Ian Grainger