472,356 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

Web Form validation with 2 forms on a single page

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?
Nov 17 '05 #1
1 4449
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()
RequiredFieldValidator1.Enabled = (Mode.Value = "Broken" Or Mode.Value
= "Login")
RequiredFieldValidator2.Enabled = (Mode.Value = "Broken" Or Mode.Value
= "Login")
RequiredFieldValidator3.Enabled = (Mode.Value = "Broken" Or Mode.Value
= "NewUser")
RequiredFieldValidator4.Enabled = (Mode.Value = "Broken" Or Mode.Value
= "NewUser")
MyBase.Validate()
End Sub

**** ASPX PAGE

<HTML>
<HEAD>
<title>ValidatedLogin1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script>

function ToLogin() {
Form1.Mode.value = "Broken"
Form1.Button1.value = "Login";
Form1.Button2.value = "New visitors click here";
Form1.TextBox1.disabled="";
Form1.TextBox2.disabled="";
Form1.TextBox3.disabled="disabled";
Form1.TextBox4.disabled="disabled";
Form1.TextBox1.style.visibility = "";
Form1.TextBox2.style.visibility = "";
Form1.TextBox3.style.visibility = "hidden";
Form1.TextBox4.style.visibility = "hidden";
ValidatorEnable(RequiredFieldValidator1, true);
ValidatorEnable(RequiredFieldValidator2, true);
ValidatorEnable(RequiredFieldValidator3, false);
ValidatorEnable(RequiredFieldValidator4, false);
Form1.TextBox3.value="";
Form1.TextBox4.value="";
Form1.Mode.value = "Login"
}

function ToNewUser() {
Form1.Mode.value = "Broken"
Form1.Button1.value = "Click here if you already have an account";
Form1.Button2.value = "Register new name and password";
Form1.TextBox1.disabled="disabled";
Form1.TextBox2.disabled="disabled";
Form1.TextBox3.disabled="";
Form1.TextBox4.disabled="";
Form1.TextBox1.style.visibility = "hidden";
Form1.TextBox2.style.visibility = "hidden";
Form1.TextBox3.style.visibility = "";
Form1.TextBox4.style.visibility = "";
ValidatorEnable(RequiredFieldValidator1, false);
ValidatorEnable(RequiredFieldValidator2, false);
ValidatorEnable(RequiredFieldValidator3, true);
ValidatorEnable(RequiredFieldValidator4, true);
Form1.TextBox1.value="";
Form1.TextBox2.value="";
Form1.Mode.value = "NewUser"
}

function LoginClick() {
if (Form1.Mode.value = "Broken")
ToLogin();
if (Form1.Mode.value = "NewUser")
ToLogin();
if (Form1.Mode.value = "Login")
{
if (typeof(Page_ClientValidate) == 'function')
{
Page_ClientValidate();
if (Page_IsValid)
Form1.submit();
}
else
{
Form1.submit()
}
}
}

function NewUserClick() {
if (Form1.Mode.value = "Broken")
ToNewUser();
if (Form1.Mode.value = "Login")
ToNewUser();
if (Form1.Mode.value = "NewUser")
{
if (typeof(Page_ClientValidate) == 'function')
{
Page_ClientValidate();
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><asp:requiredfieldvalidator
id="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"></asp:requiredfieldvalidator><BR>
<asp:textbox id="TextBox2"
runat="server"></asp:textbox><asp:requiredfieldvalidator
id="RequiredFieldValidator2" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox2"></asp:requiredfieldvalidator><BR>
<INPUT id="Button1" type="button" value="Button" name="Button1"
onclick="LoginClick();"><BR>
<hr>
<BR>
<INPUT id="Button2" type="button" value="Button" name="Button2"
onclick="NewUserClick();"><BR>
<asp:textbox id="TextBox3"
runat="server"></asp:textbox><asp:requiredfieldvalidator
id="RequiredFieldValidator3" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox3"></asp:requiredfieldvalidator><BR>
<asp:textbox id="TextBox4"
runat="server"></asp:textbox><asp:requiredfieldvalidator
id="RequiredFieldValidator4" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox4"></asp:requiredfieldvalidator>
<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.public.dotnet.framework.aspnet
NNTP-Posting-Host: bi01p1.nc.us.ibm.com 129.33.49.251
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:188679
X-Tomcat-NG: microsoft.public.dotnet.framework.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?


Nov 17 '05 #2

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

Similar topics

12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
6
by: CJM | last post by:
Can somebody clarify if/how/when a simple form is submitted when the <Enter> key is pressed? As I understood it, if you have a form with a single submit button, if enter is pressed, the form...
2
by: pv | last post by:
Hi everyone, I need help with following scenario, please: Users are accessing same web server from intranet (users previously authenticated in Active Dir) and from extranet (common public...
7
by: GeorgeAtkins | last post by:
I want to create a web-based form or page that consists of a series of formatted questions and answers. The form will resemble an existing paper form. When the form is filled in, I want the user to...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
18
by: Axel Dahmen | last post by:
Hi, trying to submit an ASPX form using the key (using IE6) the page is not submitted in my web project. Trying to debug the pages' JavaScript code I noticed that there's some ASP.NET client...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
13
by: Andrew Falanga | last post by:
HI, Just a warning, I'm a javascript neophyte. I'm writing a function to validate the contents of a form on a web page I'm developing. Since I'm a neophyte, this function is quite simple at...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.