Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NET
Home Get Started Learn Hosting Downloads Community
Forums Help
Home / ASP.NET Forums / General ASP.NET / Web Forms / Login UserName and
Password Validation
Hi all,
I have this new login page created in dot net. This is my first dot net page.
If the person clicks on login without entering a username, then it should prompt a msg
saying "Enter username"
In the next page, I am supposed to show any xml file indacting that the login is
succesful.
I started creating this page and here is the code.
I tried to create the javascript validation in the <head> tag. but it is gving some errors.
Can anyone please look at this and tell me how to do this validation... without using
asp.net validators.
<%
<!
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title></title>
function validate()
{
if (document.getElementById.txtUsername.value == "")
{
alert(
document.getElementById.txtUsername.focus();
return false;
}
return true;
}
</
script>
</
head>
<
body>
<div>
Username
<
asp:textbox ID="txtUsername" runat="server"></asp:textbox><br>
Password
<
</div>
</form>
</
body>
</
html>
Thanks,
Jennifer
Your find control command has some issue correct one is:
IF that does not work, try using single quotes around txtUsername
And as per your requirement you should handle the other text box as well so the
command will look like:
if(document.getElementById('<%=txtUsername.ClientID%>').value=="" )
document.getElementById.('<%=txtUsername.ClientID%>').focus();
else if(document.getElementById('<%=txtPassword.ClientID%>').value=="")
document.getElementById.('<%=txtPassword.ClientID%>').focus();
else
{
window.location.href("NextPage.aspx");
Hi,
gs104bently
If you want to get the textbox using javascript, you can try to use the getElementById()
method which is used to access the first element with the specified id. For your issue,
you can modify your code as follows:
<<hhttmmll xmlns="http://www.w3.org/1999/xhtml">>
<<hheeaadd runat="server">>
<<ttiittllee>><<//ttiittllee>>
<<ssccrriipptt language="javascript" type="text/javascript">>
ffuunnccttiioonn validate() {
iiff (document.getElementById("txtUsername").value
== "" & document.getElementById("txtPassword").value == "") {
alert("Name Field and Password Field cannot be
blank");
document.getElementById("txtUsername").focus();
rreettuurrnn ffaallssee;
}
iiff (document.getElementById("txtUsername").value
== "") {
alert("Name Field cannot be blank");
document.getElementById("txtUsername").focus();
rreettuurrnn ffaallssee;
}
eellssee iiff
(document.getElementById("txtPassword").value == "") {
alert("Password Field cannot be blank");
document.getElementById("txtPassword").focus();
rreettuurrnn ffaallssee;
}
eellssee {
rreettuurrnn ttrruuee;
}
}
<<//ssccrriipptt>>
<<//hheeaadd>>
<<bbooddyy>>
<<ffoorrmm id="form1" runat="server">>
<<ddiivv>>
My Smart Web Application
<<bbrr //>>
<<bbrr //>>
Username <<aasspp::tteexxttbbooxx ID="txtUsername" runat="server">>
<<//aasspp::tteexxttbbooxx>><<bbrr>>
Password <<aasspp::tteexxttbbooxx textmode="Password"
ID="txtPassword" runat="server">><<//aasspp::tteexxttbbooxx>>
<<//ddiivv>>
<<aasspp::BBuuttttoonn ID="btnLogin" runat="server" Text="Login"
OnClientClick="rreettuurrnn validate();" //>>
<<aasspp::BBuuttttoonn ID="txtReset" runat="server" Text="Reset" //>>
<<//ffoorrmm>>
<<//bbooddyy>>
<<//hhttmmll>>
gs104bently
In the next page, I am supposed to show any xml file indacting that the login is
succesful.
When you login, you can use <iframe>tag and set the src which is the address of the
xml document to embed in the <iframe>. Or bind xml file to datacontrol.
http://msdn.microsoft.com/zh-cn/library/73yw7ttd(VS.80).aspx
(http://msdn.microsoft.com/zh-cn/library/73yw7ttd(VS.80).aspx)
Best wishes,
This site is managed for Microsoft by Neudesic, LLC. | © 2018 Microsoft. All rights
reserved.