Validating two textboxes

  • Thread starter Thread starter Fred G. Sanford
  • Start date Start date
F

Fred G. Sanford

I have two text boxes. If (and only if) a user enters something in
textbox1, I need to validate that they also entered something in
Textbox2. IOW, I need to validate that they entered something in
textbox1...and if they did...validate that they entered something in
textbox2. Is there an easy way to do this, like with Validation
controls?

TIA for any help,

fgs
 
Howdy,

Yes, there is. Please find code snippet below:

-- begin aspx code --
Value A:
<asp:TextBox runat="server" ID="textBoxA" /><br />
Value B:
<asp:TextBox runat="server" ID="textBoxB" />
<asp:CustomValidator runat="server" ID="cv" EnableClientScript="true"
ValidateEmptyText="true"
ControlToValidate="textBoxB" ClientValidationFunction="ValidateValueB"
Display="Dynamic"
ErrorMessage="Please enter value B" OnServerValidate="cv_ServerValidate"
/><br />
<asp:Button runat="server" ID="submitButton" Text="Submit" />

<script type="text/javascript">
//<!--
function ValidateValueB(sender, e)
{
var valueA = document.getElementById('<%=textBoxA.ClientID %>').value;
if (ValidatorTrim(valueA) != '')
{
e.IsValid = ValidatorTrim(e.Value) != '';
}
}
//-->
</script>

<script runat="server">
// i put everything in aspx code to post it as one piece
// this should go to code behing/beside
protected void cv_ServerValidate(object source, ServerValidateEventArgs args)
{
// never trust client validation,
// someone can prepare a page to simply skip it
if (textBoxA.Text.Trim() != String.Empty)
{
args.IsValid = args.Value.Trim() != String.Empty;
}
}
</script>

-- end aspx code --

Milosz
 
Back
Top