Validating two textboxes

  • Thread starter Fred G. Sanford
  • 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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top