Validations in Asp.net using JavaScript

Validations in Asp.net using JavaScript: –>

———————————————————————————————–

Code in HTML – JavascriptValidation.aspx page file :

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title>Untitled Page</title>

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

function IsNumeric(strString)
//  check for valid numeric strings
{
var strValidChars = “0123456789.-“;
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++) {
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1) {
blnResult = false;
}
}
return blnResult;
}

function echeck(str) {

var at=”@”
var dot=”.”
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(” “)!=-1){
alert(“Invalid E-mail ID”)
return false
}

return true
}

function hi()
{
if(document.getElementById(“txtUserName”).value == “” )
{
alert(“Pls enter UserName.”);
return false;
}

if(document.getElementById(“txtName”).value == “” )
{
alert(“Pls enter Name.”);
return false;
}

if (document.getElementById(“txtPhone”).value == “”)
{
alert(“Phone can not be blank……”);
return false;
}
else
{
if(IsNumeric(document.getElementById(“txtPhone”).value) == false  )
{
alert(‘enter only numeric value.’);
return false;
}

}

if(echeck(document.getElementById(“txtEmail”).value) == false)
{
//alert(“Invalid email”);
return false;
}

}

</script>

</head>
<body>
<form id=”form1″ runat=”server”>
<div>

<table border=”1″ style=”width:100%;”>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
UserName</td>
<td>
<asp:TextBox ID=”txtUserName” runat=”server”></asp:TextBox>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
Phone</td>
<td>
<asp:TextBox ID=”txtPhone” runat=”server”></asp:TextBox>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
Email</td>
<td>
<asp:TextBox ID=”txtEmail” runat=”server”></asp:TextBox>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID=”Button1″ runat=”server” onclientclick=”return hi()”
Text=”Submit”  />
</td>
<td>
&nbsp;</td>
</tr>
</table>

</div>
</form>
</body>
</html>

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s