show-notice
hide-notice

Wednesday, 26 June 2013

Javascript Validations to Validate Form in ASP.NET


Introduction

Here I will explain Form validation used to occur at the server, after the client had entered all necessary data and then pressed the Submit button. If some of the data that had been entered by the client had been in the wrong form or was simply missing, the server would have to send all the data back to the client and request that the form be resubmitted with correct information. This was really a lengthy process and over burdening server.




Examples




<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Registration </title>

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

function validation()
{
if(document.getElementById("<%=fntxt.ClientID%>").value=="")
{
alert("Enter first name");
document.getElementById("<%=fntxt.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=lntxt.ClientID%>").value=="")
{
alert("Enter Last Name");
document.getElementById("<%=lntxt.ClientID %>").focus();
return false;

}
if(document.getElementById("<%=comtxt.ClientID%>").value=="")
{
alert("Enter company Name");
document.getElementById("<%=comtxt.ClientID %>").focus();
return false;
}

if(document.getElementById("<%=deptxt.ClientID%>").value=="")
{
alert("Enter department Name");
document.getElementById("<%=deptxt.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=ttltxt.ClientID%>").value=="")
{
alert("Enter Title");
document.getElementById("<%=ttltxt.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=cttxt.ClientID%>").value=="")
{
alert("Enter City");
document.getElementById("<%=cttxt.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=sttxt.ClientID%>").value=="")
{
alert("Enter State");
document.getElementById("<%=sttxt.ClientID%>").focus();
return false;
}

var email=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var emailid=document.getElementById("<%=emailtxt.ClientID%>").value;
var emailmatch=emailid.match(email);
if(emailmatch==null)
{
alert("Email address incorrect");
document.getElementById("<%=emailtxt.ClientID%>").focus();
return false;

}
if(document.getElementById("<%=passtxt.ClientID%>").value=="")
{
alert("Enter Password");
document.getElementById("<%=passtxt.ClientID%>").focus();
return false;
}
var pass=document.getElementById("passtxt");
var pass1=document.getElementById("conpasstxt");

if(pass.value!=pass1.value)
{
alert("password not match");
return false;
}

}





    </script>
  

</head>
<body><form id="form1"  runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<center>
<table>
<tr>
<td>

</td>
<td>
<asp:DropDownList ID="solddl" runat="server">
<asp:ListItem Value="1">Mr.</asp:ListItem>
<asp:ListItem Value="2">Miss.</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="fntxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="lntxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Company/Institution:
</td>
<td>
<asp:TextBox ID="comtxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department:
</td>
<td>
<asp:TextBox ID="deptxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Title:
</td>
<td>
<asp:TextBox ID="ttltxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<asp:DropDownList ID="counddl" runat="server">
<asp:ListItem Value="1">United States</asp:ListItem>
<asp:ListItem Value="2">India</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
City or Town:
</td>
<td>
<asp:TextBox ID="cttxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
State or Province:
</td>
<td>
<asp:TextBox ID="sttxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email Address:
</td>
<td>
<asp:TextBox ID="emailtxt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="passtxt" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<asp:PasswordStrength ID="PswStrength" TargetControlID="passtxt" StrengthIndicatorType="Text"
runat="server" PrefixText="Passrord Strenth : ">
</asp:PasswordStrength>
<tr>
<td>
Confirm Password:
</td>
<td>
<asp:TextBox ID="conpasstxt" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="svbtn" OnClientClick="return validation();"   OnClick="svbtn_Click" runat="server"  Text="Save" />
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>






SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com