Sei sulla pagina 1di 8

ASP.

NET
Home Get Started Learn Hosting Downloads Community

Forums Help

Home / ASP.NET Forums / General ASP.NET / Web Forms / Login UserName and
Password Validation

Login UserName and Password


Validation [Answered]
4 replies
Last post Dec 19, 2012 01:20 AM by Catherine Shan - MSFT

Login UserName and Password Validation


Dec 12, 2012 06:24 PM | gs104bently

Hi all,

I have this new login page created in dot net. This is my first dot net page.

I  have to introduce validation for username and password.

If the person clicks on login without entering a username, then it should prompt a msg
saying "Enter username"

Same for password.

This is at the client side.

I can only use TextBox and Button

I am asked to "Use JavaScript for validation (do not use ASP.NET


validators)"

I am asked to use "The minimum validation should ensure that both


login and password fields are entered. Anything extra is a plus."

      Once login is successful redirect to the next page

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.

It shows the username and password box and login button.

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.

No server side validation is required at this time.

<%

@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"


Inherits="WebApplication1.WebForm1" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org


/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

html xmlns="http://www.w3.org/1999/xhtml">

<

head runat="server">

   

<title></title>

    

<script language="javascript" type="text/javascript">

        

function validate()

          {
             

if (document.getElementById.txtUsername.value == "")

              {

                 alert(

"Name Field cannot be blank");

                 document.getElementById.txtUsername.focus();

                

return false;

             }

            

return true;

         }

</

script>

</

head>

<

body>

   

<form id="form1" runat="server">

   

<div>

    My Smart Web Application

<br /><br />

     Username

    

<
asp:textbox ID="txtUsername" runat="server"></asp:textbox><br>

Password

   

<

asp:textbox textmode=password ID="txtPassword" runat="server"></asp:textbox>

   

</div>

   

   

<asp:Button ID="btnLogin" runat="server" Text="Login"  OnClientClick="return


validate();"  />

<asp:Button ID="txtReset" runat="server" Text="Reset" />

   

</form>

</

body>

</

html>

Thanks,

Jennifer

Re: Login UserName and Password Validation


Dec 14, 2012 01:21 AM | usman400

Your find control command has some issue correct one is:

iiff (document.getElementById("txtUsername").value == "")

IF that does not work, try using single quotes around txtUsername

Re: Login UserName and Password Validation


Dec 14, 2012 01:22 AM | usman400

And as per your requirement you should handle the other text box as well so the
command will look like:

iiff ( document.getElementById("txtUsername").value == "" ||


document.getElementById("txtUserpassword").value == "")

Re: Login UserName and Password Validation


Dec 14, 2012 01:23 AM | RameshRajendran

if(document.getElementById('<%=txtUsername.ClientID%>').value==""  )

//AlertMessge here for UserName empty

document.getElementById.('<%=txtUsername.ClientID%>').focus();

else if(document.getElementById('<%=txtPassword.ClientID%>').value=="")

//AlertMessge here for Password empty

document.getElementById.('<%=txtPassword.ClientID%>').focus();

else

{
window.location.href("NextPage.aspx");

Re: Login UserName and Password Validation


Dec 19, 2012 01:20 AM | Catherine Shan - MSFT

Hi,

gs104bently

I am asked to "Use JavaScript for validation (do not use ASP.NET


validators)"

I am asked to use "The minimum validation should ensure that


both login and password fields are entered. Anything extra is a
plus."

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.

Please refer to:

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.

Potrebbero piacerti anche