I have three textboxes and I want to validate them. At least one textbox must contain data.
How can I do this?
(The textboxes are Home Phone No., Work Phone No., Mobile No. and I need to check at least one method of contact is specified)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
<script language="javascript">
function Validate(sender, args){
args.IsValid = false;
if(args.Value != "")
{
args.IsValid = true;
}}</script>
the above function dont validate that at least one textbox contains data it validate that the control attached to the validator have 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>
In case 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/
Method 2
Use a Custom validator, with ClientValidationFunction property to 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;
}
Method 3
Use a Custom Validator, there is no need to cycle through the text boxes on the page as this approach gets ALL of the text boxes on the page. The JavaScript function specified in the ClientValidationFunction will be called for each text box with a validator associated with it.
<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>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0