Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Javascript validations to validate form in asp.net

Oct 22, 2010
Introduction 

Here I will explain how to validate form using JavaScript validations in asp.net 

Description

I have a one sample registration form that contains fields like Username, Password, Confirm Password, First Name, Last Name, Email, PhoneNo and Location.
Now I want to check whether user enter data in all fields or not and I want to check whether user enter correct data in all fields or not by using JavaScript.

If I want to check whether user enters data in textbox or not write script like this 


function isvaliduser()
{
var uid;
var temp=document.getElementById("<%=txtuser.ClientID %>");
uid=temp.value;
if(uid=="")
{
alert ("Please Enter UserName”);
return false;
}
else
{
return true;
}
}
Write the following code aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Vallidations Page</title>
<script language="javascript" type="text/javascript">
function validate() {
var summary = "";
summary += isvaliduser();
summary += isvalidFirstname();
summary += isvalidLocation();
if (summary != "") {
alert(summary);
return false;
}
else {
return true;
}

}
function isvaliduser() {
var uid;
var temp = document.getElementById("<%=txtuser.ClientID %>");
uid = temp.value;
if (uid == "") {
return ("Please Enter UserName" + "\n");
}
else {
return "";
}
}
function isvalidFirstname() {
var uid;
var temp = document.getElementById("<%=txtfname.ClientID %>");
uid = temp.value;
if (uid == "") {
return ("Please enter firstname" + "\n");
}
else {
return "";
}
}
function isvalidLocation() {
var uid;
var temp = document.getElementById("<%=txtlocation.ClientID %>");
uid = temp.value;
if (uid == "") {
return ("Please enter Location" + "\n");
}
else {
return "";
}
}
</script>
</head>
<body>
<form id="form1" runat="server"> 
<table align="center">
<tr>
<td>
<asp:Label ID="lbluser" runat="server" Text="Username"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblfname" runat="server" Text="FirstName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbllocation" runat="server" Text="Location"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtlocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Save"
OnClientClick ="javascript:validate()" />
</td>
</tr>
</table>
</form>
</body>
</html>
Demo



If you want validation for emial and website url Here is the link

http://aspdotnet-suresh.blogspot.com/2010/09/validation-for-email-and-url-using-in.html

Happy Coding

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Anonymous said...

In the button event, I'm redirecting the page to another link. The page is redirecting even with the empty textboxes? The return false in the java script is not working?

Anonymous said...

thanks....this really helps me.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.