Code-:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>Vallidations Page</title>
<script language="javascript" type="text/javascript">
function validate() {
var summary = "";
summary += isvalidFirstname();
summary += isvalidEmail();
summary += isvalidphoneno();
if (summary != "") {
alert(summary);
return false;
}
else {
return true;
}
}
function isvalidphoneno() {
var uid;
var temp = document.getElementById("<%=txtphone.ClientID %>");
uid = temp.value;
var re;
re = /^[0-9]+$/;
var digits = /\d(10)/;
if (uid == "") {
return ("Enter Ur Phone No." + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Only Numbers Allowed" + "\n");
}
}
function isvalidFirstname() {
var uid;
var temp = document.getElementById("<%=txtfname.ClientID %>");
uid = temp.value;
var re=/^[a-zA-Z ]+$/
if (uid == "") {
return ("Enter First Name" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Characters and spaces only" + "\n");
}
}
function isvalidEmail() {
var uid;
var temp = document.getElementById("<%=txtEmail.ClientID %>");
uid = temp.value;
var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (uid == "") {
return ("Please Enter Email" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Enter A valid Email" + "\n");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<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="Label1" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCnt" runat="server" Text="Phone No"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtphone" 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-:
I always forget you can refer to the. Net controls directly in the JavaScript. I usually inject it from the code-behind. Nice functions thanks.
ReplyDeleteDana
Ur Welcome
Deletemay I know the use of "test"
ReplyDeletein the "else if (re.test(uid))"