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
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. |
|||
|
|||
17 comments :
Its very good article
varaprasad
Thats Exactly wat I was looking for,
Thanks alot
Awesome ....Great Help for meeee... :) Keep it up..
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..
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
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
very Nice Thanks :)
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..
that is really nice....... I want total c# and ASP.net article. please provide all topics in your blog
i want code for text box not allowing special characters i have a code but not work in firefox
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
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
Very insightful about JavaScript validations!
Good, I will try this in asp.net
Very Thanks For Email Validations
thanks for this post.i really like this post
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
Note: Only a member of this blog may post a comment.