I am writing an asp.net application for a benchmark and I wanted to know if there is a validator that will test if 2 text boxes have a value and if both text boxes do not a have value the validator will fire.
I am writing an asp.net application for a benchmark and I wanted to know if there is a validator that will test if 2 text boxes have a value and if both text boxes do not a have value the validator will fire.
You can use custom validator.. or use JS script to validate. Something like this..
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return ValidateTextBox();" />
<script type="text/javascript" language="javascript">
function ValidateTextBox()
{
var txt1 = document.getElementById('<%= TextBox1.ClientID %>');
var txt2 = document.getElementById('<%= TextBox2.ClientID %>');
if( txt1.value == "" || txt2.value == "")
{
alert('Enter info for atleast one!');
return false;
}
else
{return true;}
}
</script>
sidy3d
Member
1 Points
4 Posts
Validator to test if one of 2 text boxes have a value
Aug 02, 2012 06:11 PM|LINK
Hello,
I am writing an asp.net application for a benchmark and I wanted to know if there is a validator that will test if 2 text boxes have a value and if both text boxes do not a have value the validator will fire.
Thanks,
tarunSaini
Contributor
2948 Points
985 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 02, 2012 06:13 PM|LINK
post your code
sidy3d
Member
1 Points
4 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 02, 2012 06:17 PM|LINK
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { if (txtsomething.text == null && txtRtsomething.Text == null) { args.IsValid = true; } else { args.IsValid = false; } }budugu
All-Star
41108 Points
6019 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 02, 2012 06:17 PM|LINK
You can use custom validator.. or use JS script to validate. Something like this..
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return ValidateTextBox();" /> <script type="text/javascript" language="javascript"> function ValidateTextBox() { var txt1 = document.getElementById('<%= TextBox1.ClientID %>'); var txt2 = document.getElementById('<%= TextBox2.ClientID %>'); if( txt1.value == "" || txt2.value == "") { alert('Enter info for atleast one!'); return false; } else {return true;} } </script>"Don't be afraid to be wrong; otherwise you'll never be right."
sidy3d
Member
1 Points
4 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 02, 2012 06:44 PM|LINK
Thanks,
Do you know where I should specify the 2 textboxes to validate.
Thanks Again,
budugu
All-Star
41108 Points
6019 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 03, 2012 01:07 AM|LINK
Just change textbox id's in following code..
var txt1 = document.getElementById('<%= TextBox1.ClientID %>'); var txt2 = document.getElementById('<%= TextBox2.ClientID %>');"Don't be afraid to be wrong; otherwise you'll never be right."
sidy3d
Member
1 Points
4 Posts
Re: Validator to test if one of 2 text boxes have a value
Aug 03, 2012 04:06 PM|LINK
Thank You,
Do you know why my server side validator is not working?
Thanks in advance.
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { if (txtsomething.text == null && txtRtsomething.Text == null) { args.IsValid = true; } else { args.IsValid = false; } }