Asp.net validate textbox - at least one textbox must have data in

I have three text boxes and I want to check them. At least one text field must contain data.

How can i do this?

(The text box is home phone number, work phone number, mobile number and I need to check at least one contact method)

+2


source to share


3 answers


Use a custom validator, there is no need to loop through the text boxes on the page as this approach gets ALL the text boxes on the page. The JavaScript function specified in ClientValidationFunction

will be called for each text field with an associated validator.



<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvHomePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtHomePhone"  ValidateEmptyText="true"></asp:CustomValidator>


<asp:TextBox ID="txtWorkPhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvWorkPhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtWorkPhone"  ValidateEmptyText="true"></asp:CustomValidator>


<asp:TextBox ID="txtMobilePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvMobilePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtMobilePhone"  ValidateEmptyText="true"></asp:CustomValidator>


<script language="javascript">
function Validate(sender, args)
{
    args.IsValid = false;
    if(args.Value != "")
    {
        args.IsValid = true;
    }
}
</script>

      

0


source


<script language="javascript">
     function Validate(sender, args){    
       args.IsValid = false;    
       if(args.Value != "")    
       {        
         args.IsValid = true;    
    }}</script>

      

the above function does not check that at least one textbox contains data that confirms that the control attached to the validator has data. Just use one custom validator like this



<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<asp:TextBox ID="txtWorkPhone" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMobilePhone" runat="server"></asp:TextBox>

<asp:CustomValidator ID="cvMobilePhone" runat="server" ErrorMessage="ADASDASDA" ClientValidationFunction="Validate"
  ValidateEmptyText="true"></asp:CustomValidator>

<script language="JavaScript">
  function Validate(sender, args) {
    var txt1 = document.getElementById("<%= txtHomePhone.ClientID %>");
    var txt2 = document.getElementById("<%= txtWorkPhone.ClientID%>");
    var txt3 = document.getElementById("<%= txtMobilePhone.ClientID%>");
    args.IsValid = (txt1.value != "") || (txt2.value != "") || (txt3.value != "");
  }
</script>

      

If you want to reuse the function, you can add attributes to your validation object. Check it out: http://alejandrobog.wordpress.com/2009/09/27/pass-your-own-arguments-to-the-clientvalidationfunction-in-a-customvalidator/

+9


source


Use a custom validator with the ClientValidationFunction property for this function.

 function validate(source, arguments) {
            var textboxes = document.getElementsByTagName("INPUT");
            for (var i = 0; i < textboxes.length; i++) {
                if (textboxes[i].type == "text" && textboxes[i].value != "") {
                    arguments.IsValid = true;
                    return;
                }
            }
            arguments.IsValid = false;
        }

      

+2


source







All Articles