19765604
Hi,
On the client, you need to use a function named ValidatorEnable to turn
specific validators on or off. On the server, you need to override the
Validate event and add code to enable/disable specific validators.
Notice that I'm careful to replicate the enable/disable state of each
validator in the server Validate event to be the same state as it was on
the client. Errors here could cause bugs that are hard to track down.
The following sample uses two text boxes for users to login & two for new
users to sign up. All four have required field validators. (with this
simple example, it would have been more elegant to use just one set of two
text boxes. However, I expect that your login & sign-up sections are
different from each other).
This whole sample has almost no error handling since it's just a sample.
The "broken" mode merely turns on all validators on the server & blocks
almost any activity,
**** CODE-BEHIND
Public Overrides Sub Validate()
RequiredFieldVa lidator1.Enable d = (Mode.Value = "Broken" Or Mode.Value
= "Login")
RequiredFieldVa lidator2.Enable d = (Mode.Value = "Broken" Or Mode.Value
= "Login")
RequiredFieldVa lidator3.Enable d = (Mode.Value = "Broken" Or Mode.Value
= "NewUser")
RequiredFieldVa lidator4.Enable d = (Mode.Value = "Broken" Or Mode.Value
= "NewUser")
MyBase.Validate ()
End Sub
**** ASPX PAGE
<HTML>
<HEAD>
<title>Validate dLogin1</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
<script>
function ToLogin() {
Form1.Mode.valu e = "Broken"
Form1.Button1.v alue = "Login";
Form1.Button2.v alue = "New visitors click here";
Form1.TextBox1. disabled="";
Form1.TextBox2. disabled="";
Form1.TextBox3. disabled="disab led";
Form1.TextBox4. disabled="disab led";
Form1.TextBox1. style.visibilit y = "";
Form1.TextBox2. style.visibilit y = "";
Form1.TextBox3. style.visibilit y = "hidden";
Form1.TextBox4. style.visibilit y = "hidden";
ValidatorEnable (RequiredFieldV alidator1, true);
ValidatorEnable (RequiredFieldV alidator2, true);
ValidatorEnable (RequiredFieldV alidator3, false);
ValidatorEnable (RequiredFieldV alidator4, false);
Form1.TextBox3. value="";
Form1.TextBox4. value="";
Form1.Mode.valu e = "Login"
}
function ToNewUser() {
Form1.Mode.valu e = "Broken"
Form1.Button1.v alue = "Click here if you already have an account";
Form1.Button2.v alue = "Register new name and password";
Form1.TextBox1. disabled="disab led";
Form1.TextBox2. disabled="disab led";
Form1.TextBox3. disabled="";
Form1.TextBox4. disabled="";
Form1.TextBox1. style.visibilit y = "hidden";
Form1.TextBox2. style.visibilit y = "hidden";
Form1.TextBox3. style.visibilit y = "";
Form1.TextBox4. style.visibilit y = "";
ValidatorEnable (RequiredFieldV alidator1, false);
ValidatorEnable (RequiredFieldV alidator2, false);
ValidatorEnable (RequiredFieldV alidator3, true);
ValidatorEnable (RequiredFieldV alidator4, true);
Form1.TextBox1. value="";
Form1.TextBox2. value="";
Form1.Mode.valu e = "NewUser"
}
function LoginClick() {
if (Form1.Mode.val ue = "Broken")
ToLogin();
if (Form1.Mode.val ue = "NewUser")
ToLogin();
if (Form1.Mode.val ue = "Login")
{
if (typeof(Page_Cl ientValidate) == 'function')
{
Page_ClientVali date();
if (Page_IsValid)
Form1.submit();
}
else
{
Form1.submit()
}
}
}
function NewUserClick() {
if (Form1.Mode.val ue = "Broken")
ToNewUser();
if (Form1.Mode.val ue = "Login")
ToNewUser();
if (Form1.Mode.val ue = "NewUser")
{
if (typeof(Page_Cl ientValidate) == 'function')
{
Page_ClientVali date();
if (Page_IsValid)
Form1.submit();
}
else
{
Form1.submit()
}
}
}
</script>
</HEAD>
<body onload="ToLogin ();">
<form id="Form1" method="post" runat="server">
<P><INPUT id="Mode" type="hidden" name="Mode" runat="server">
<br>
<asp:textbox id="TextBox1"
runat="server"> </asp:textbox><as p:requiredfield validator
id="RequiredFie ldValidator1" runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:requiredfie ldvalidator><BR >
<asp:textbox id="TextBox2"
runat="server"> </asp:textbox><as p:requiredfield validator
id="RequiredFie ldValidator2" runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox2"> </asp:requiredfie ldvalidator><BR >
<INPUT id="Button1" type="button" value="Button" name="Button1"
onclick="LoginC lick();"><BR>
<hr>
<BR>
<INPUT id="Button2" type="button" value="Button" name="Button2"
onclick="NewUse rClick();"><BR>
<asp:textbox id="TextBox3"
runat="server"> </asp:textbox><as p:requiredfield validator
id="RequiredFie ldValidator3" runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox3"> </asp:requiredfie ldvalidator><BR >
<asp:textbox id="TextBox4"
runat="server"> </asp:textbox><as p:requiredfield validator
id="RequiredFie ldValidator4" runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox4"> </asp:requiredfie ldvalidator>
<P></P>
<P></P>
</form>
</body>
</HTML>
Thank you, Mike
Microsoft, ASP.NET Support Professional
Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
From: "iMedia User" <xx*@us.ibm.com >
Subject: Web Form validation with 2 forms on a single page
Date: Wed, 5 Nov 2003 07:55:28 -0500
Lines: 10
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <eI************ **@TK2MSFTNGP12 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
NNTP-Posting-Host: bi01p1.nc.us.ib m.com 129.33.49.251
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1886 79
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
I have a site where I want to use the Web form validators in two separate
forms on a single page. One form allows existing users to log in while the
second one allows new users to register. The problem is that if I use the
Web Form validation tags, it treats them all as one form (ie...if someone
is trying to use the login form, they get an error saying that they have to
enter values in the registration form and vice-versa).
Any ideas on how to handle this?