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

Regular expression to validate phone number using jquery in asp.net

Jul 23, 2012
Introduction

Here I will explain how to validate phone number with regular expression using Jquery in asp.net.

Description:
  
In previous articles I explained validate email address using JQuery in asp.net and many articles relating to JQuery. Now I will explain how to use regular expression to validate phone number using JQuery in asp.net.

To implement this one we need to write code as shown below in your aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Regular expression to validate phone number using jquery in asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnValidate").click(function () {
var phoneText = $("#txtPhone").val();
if ($.trim(phoneText).length == 0) {
alert("Please enter Phone Number");
return false;
}
if (validatePhone(phoneText)) {
alert('Valid Phone Number');
return true;
}
else {
alert('Invalid Phone Number');
return false;
}
});
});
function validatePhone(phoneText) {
var filter = /^[0-9-+]+$/;
if (filter.test(phoneText)) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="txtPhone" runat="server" />
<asp:Button ID="btnValidate" runat="server" Text="Validate" />
</div>
</form>
</body>
</html>
If you observe above code in header section I added script file link by using that file we have a chance to interact with JQuery and in the script we have btnValidate button click function which is used to validate phone number based using JQuery in asp.net.

Demo


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

5 comments :

anant said...

I want Image Preview before upload in asp.net plz send me this code

arun rote said...

I am using you code to do jquery validation for number and required fields.

I need regular express to validation US Zip code.
I am using this one
return this.optional(element) || /^\d{5}\-$|^\d{5}\-\d{4}$/.test(value);

when i enter only 5 digits it is not working.

arun rote said...

I am using you code to do jquery validation for number and required fields.

I need regular express to validation US Zip code.
I am using this one
return this.optional(element) || /^\d{5}\-$|^\d{5}\-\d{4}$/.test(value);

when i enter only 5 digits it is not working.

Abhi Gupta said...

I want the code for the mail confirmation means if the user forgot its password then by entering his mail id the password is sent ti his mail. myid is abhigupta3189@gmail.com Thanks in advance.

Rahul Kedare said...

Hiii Suresh Sir,

I want the validation in c# and VB.net like this
if we enter only 2 numbers in the text it throw an error and only 10 numbers can insert and which can strats with 9,8,7 like this

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.