TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
JavaScript Validation For Login Form using ASP.NET
Amit Sanandiya
Apr 26
2015
Code
8.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<
script
type
=
"text/javascript"
>
// Main Function for all validation
function AllInOnevalidate()
{
var
ValidationSummary
=
""
;
ValidationSummary += NameValidat();
ValidationSummary += EmailValidat();
ValidationSummary += PhonenoValidat();
ValidationSummary += PasswordValidat();
if (ValidationSummary != "")
{
alert(ValidationSummary);
return false;
}
else
{
alert("Submited Successfuly");
return true;
}
}
// For Name validation
function NameValidat()
{
var userid;
var
controlId
=
document
.getElementById("
<
%=txtcontct.ClientID %
>
");
userid
=
controlId
.value;
var val;
val
= /^[0-9]+$/;
var
digits
= /\d(10)/;
if (
userid
== "")
{
return ("PhoneNo Must Require" + "\n");
}
else if (val.test(userid))
{
return "";
}
else
{
return ("PhoneNo should be only in digits" + "\n");
}
}
// For Email validation
function EmailValidat()
{
var userid;
var
controlId
=
document
.getElementById("
<
%=txtname.ClientID %
>
");
userid
=
controlId
.value;
var
val
= /^[a-zA-Z ]+$/
if (
userid
== "")
{
return ("Name Must Require" + "\n");
}
else if (val.test(userid))
{
return "";
}
else
{
return ("Name accepts only spaces and charcters" + "\n");
}
}
// For Phone validation
function PhonenoValidat()
{
var userid;
var
controlId
=
document
.getElementById("
<
%=txtemail.ClientID %
>
");
userid
=
controlId
.value;
var
val
= /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (
userid
== "")
{
return ("Email Id Must Require" + "\n");
}
else if (val.test(userid))
{
return "";
}
else
{
return ("Email should be in this form example:
[email protected]
" + "\n");
}
}
// For Password validation
function PasswordValidat()
{
var
password
=
document
.getElementById('
<
%=txtPassword.ClientID %
>
').value;
var
score
=
0
;
if (
password.length
== 0)
{
return ("Please Enter Password" + "\n");
}
else if (password.length
<
8
)
{
return ("Password should be 8 chr" + "\n");
}
else
{
//for small letters
for (var
s
=
0
; s
<
password.length
; ++s)
{
if (password.charCodeAt(s)
>
= 'a'.charCodeAt(0) && password.charCodeAt(s)
<
= 'z'.charCodeAt(0))
{
score += 1;
break;
}
}
//for capital letters
for (var
s
=
0
; s
<
password.length
; ++s)
{
if (password.charCodeAt(s)
>
= 'A'.charCodeAt(0) && password.charCodeAt(s)
<
= 'Z'.charCodeAt(0))
{
score += 1;
break;
}
}
//for numbers
for (var
s
=
0
; s
<
password.length
; ++s)
{
if (password.charCodeAt(s)
>
= '0'.charCodeAt(0) && password.charCodeAt(s)
<
= '9'.charCodeAt(0))
{
score += 1;
break;
}
}
if (
score
==3)
{
return "";
}
else
{
return ("Password should be Small Latter,Capital Latter and Digit" + "\n");
}
};
</
script
>
//add control for design view
<
table
style
="border-color: #333300; z-index: 1; left: 15px; top: 54px; position: absolute;
height: 122px; width: 361px"
>
<
tr
>
<
td
class
=
"style2"
>
<
asp:Label
ID
=
"lblname"
runat
=
"server"
Text
=
"Full Name"
>
</
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtname"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
class
=
"style2"
>
<
asp:Label
ID
=
"lblemail"
runat
=
"server"
Text
=
"Email"
>
</
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtemail"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
class
=
"style2"
>
<
asp:Label
ID
=
"lblCntno"
runat
=
"server"
Text
=
"Phone No"
>
</
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtcontct"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
class
=
"style2"
>
Password
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtPassword"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
class
=
"style3"
>
</
td
>
<
td
class
=
"style1"
>
<
asp:Button
ID
=
"bttnsubmit"
runat
=
"server"
Font-Bold
=
"True"
OnClientClick
=
"javascript:AllInOnevalidate()"
Text
=
"Submit"
onclick
=
"bttnsubmit_Click"
/>
</
td
>
</
tr
>
</
table
>
ASP.NET
Login Form Validation