Wednesday, March 16, 2011

javascript validations for Email,Phone no,to allow only characters and spaces and for allow only numbers in asp.net


Introduction 

Here I will explain how to validate Email,phone number,to allow only characters and spaces and for allow only numbers using JavaScript validations in asp.net 

Description

I have a one sample registration form that contains fields like Name, Email, PhoneNo .
Now I want to check whether user enter correct email format or not, and phone number contains only numbers or not and name contains only characters and spaces or not by using JavaScript.

Here some of commonly used regular expressions


Regular Expression for Email
re=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

Regular expression to accept only characters and spaces

re=/^[a-zA-Z ]+$/

Regular expression to accept alphanumeric and spaces

re=/^[0-9a-zA-Z ]+$/

Regular Expression to accept only numbers 

re =/^[0-9]+$/;

Regular Expression for Mobile Number

re=/\d(10)/;

MarkUp code 


<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 ("Please enter phoneno" + "\n");
}
else if (re.test(uid)) {
return "";
}

else {
return ("Phoneno should be digits only" + "\n");
}
}
function isvalidFirstname() {
var uid;
var temp = document.getElementById("<%=txtfname.ClientID %>");
uid = temp.value;
var re=/^[a-zA-Z ]+$/
if (uid == "") {
return ("Please enter firstname" + "\n");
}
else if (re.test(uid)) {
return "";

}
else {
return ("FirstName accepts 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 ("Email should be in the form ex:abc@xyz.com" + "\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


No comments:

Post a Comment