<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaScriptValidations.aspx.cs" Inherits="JavaScriptValidations" %>
<!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">
<script language="javascript" type="text/javascript">
function validate()
{
if (document.getElementById("txtName").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("txtName").focus();
return false;
}
if (document.getElementById("txtEmail").value == "")
{
alert("Email id can not be blank");
document.getElementById("txtEmail").focus();
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid = document.getElementById("txtEmail").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("txtEmail").focus();
return false;
}
//WebUrl Validation
if (document.getElementById("txtWebURL").value == "")
{
alert("Web URL can not be blank");
document.getElementById("txtWebURL").value = "http://"
document.getElementById("txtWebURL").focus();
return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL = document.getElementById("txtWebURL").value;
var matchURL=tempURL.match(Url);
if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("txtWebURL").focus();
return false;
}
//Zip Validation
if (document.getElementById("txtZIP").value == "")
{
alert("Zip Code is not valid");
document.getElementById("txtZIP").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i = 0; i < document.getElementById("txtZIP").value.length; i++)
{
temp = document.getElementById("txtZIP").value.substring(i, i + 1);
if (digits.indexOf(temp)==-1)
{
alert("Please enter correct zip code");
document.getElementById("txtZIP").focus();
return false;
}
}
return true;
}
</script>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Email:<asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox><br />
Web URL : <asp:TextBox ID="txtWebUrl" runat="server" ></asp:TextBox><br />
Zip : <asp:TextBox ID="txtZip" runat="server"></asp:TextBox> <br />
<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" /><br />
</div>
</form>
</body>
</html>