473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Validating two textboxes

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

Feb 9 '07 #1
2 1157
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
"Fred G. Sanford" wrote:
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

Feb 10 '07 #2
On Feb 9, 6:09 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Howdy,

Yes, there is. Please find code snippet below:

Thanks Milosz. Works great! You da man!

Feb 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1110
by: blurz | last post by:
Hi, would like to know how to go about validating a a group of textboxes. For example, I have 3 textboxes that will hold a user's different contact numbers. How do I ensure that at least one of...
6
5765
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
1577
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works...
0
2434
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
7
1729
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
6
4727
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
0
1292
by: Rolf | last post by:
I create several textboxes in code on the fly, using the same function. I attach a validating event in that function that calls a validating routine.. but somehow on some textboxes the validating...
2
1180
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question...
1
1291
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question...
8
1571
by: clintonG | last post by:
I have to get to this later tonight or tommorrow...and wonder... There's two TextBoxes in a Wizard Step posing an either-or situtation. Only one or the other TextBox may pass data. The...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.