Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

Oct 22, 2010
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)/;
Write following code in aspx page like this


<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


For JavaScript other post check these links

http://www.aspdotnet-suresh.com/2010/10/javascript-validations-to-validate-form.html

http://www.aspdotnet-suresh.com/2010/09/validation-for-email-and-url-using-in.html

Happy Coding

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

17 comments :

varaprasad said...

Its very good article

varaprasad

Anonymous said...

Thats Exactly wat I was looking for,

Thanks alot

Aruna Perera said...

Awesome ....Great Help for meeee... :) Keep it up..

Sharath Pannala said...

it's a good article.....
but i have some problem when I have more than one button in the form...so how to validate controls with single button(say for instance if I have submit and cancel buttons)...please help me out..

Ramu said...

thank you suresh it was very help full.
but i add a content page when i am using master page, it's not working where should i write
all these javascript code

Ramu said...

thank you suresh it was very help full.
but i add a content page when i am using master page, it's not working where should i write
all these javascript code

Anonymous said...

very Nice Thanks :)

Anonymous said...

I need regular expression to accept 11mit001 these kind characters only.....And not other characters like abc instead of mit...it should be 11 first followed by 11 should be mit small or caps and digits...pls help..thanks in advance..

MallaReddy said...

that is really nice....... I want total c# and ASP.net article. please provide all topics in your blog

Unknown said...

i want code for text box not allowing special characters i have a code but not work in firefox

neha jain said...

this article was very helpful..one quick query--
can we also use maxlength property to limit number of digits in the textbox rather than using "var digits = /\d(10)/;"??
and what is the use of this variable? it is not called anywhere??
Thanks,
Neha

neha jain said...

one more query, how should we understand "/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;" ?
Appreciate,if you could please elaborate the meaning of this expression.
Thanks in advance.
Regards,Neha

hermes said...

Very insightful about JavaScript validations!

log in said...

Good, I will try this in asp.net

Anonymous said...

Very Thanks For Email Validations

mysql services said...

thanks for this post.i really like this post

Some thing said...

Thanks for this post but enter phone number in 10digits only. some times enter 8numbers can see the invalid number then how to use code in html,xml,java script etc

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.