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 Convert Phone Number Format Example in jQuery

May 3, 2016
Introduction

Here I will explain how to use
JavaScript to format phone number with example or convert phone number to international format (USA) in JavaScript / jQuery convert number to phone number format with example or JavaScript phone number validation example. By loop through number we can convert number to phone number format.


To convert number to phone number / international format in JavaScript we need to write the code like as shown below


<script type="text/javascript">
function convertphone() {
var phnumber = document.getElementById('txtnumber').value;
var numbers = phnumber.replace(/\D/g, ''),
char = { 0: '(', 3: ') ', 6: ' - ' };
phnumber = '';
for (var i = 0; i < numbers.length; i++) {
phnumber += (char[i] || '') + numbers[i];
}
document.getElementById('lblResult').innerHTML = phnumber;
}
</script>

If you observe above code we are getting textbox value and converting number in (xxx) xxx – xxxx format.

Now we will see complete example of converting number to phone number format for that we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JavaScript Convert Phone Number Example</title>
<script type="text/javascript">
function convertphone() {
var phnumber = document.getElementById('txtnumber').value;
var numbers = phnumber.replace(/\D/g, ''),
char = { 0: '(', 3: ') ', 6: ' - ' };
phnumber = '';
for (var i = 0; i < numbers.length; i++) {
phnumber += (char[i] || '') + numbers[i];
}
document.getElementById('lblResult').innerHTML = phnumber;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Number: <input type="text" id="txtnumber" value="91042654380" />
<input type="button" value="Convert" onclick="convertphone()" /><br /><br />
International Format : <label id="lblResult"></label>
</div>
</form>
</body>
</html>

Live Demo

For live demo click on below button to convert textbox number to phone number (international US) format.


Enter Number:

International Format :

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

1 comments :

Unknown said...

phnumber.replace(/\D/g, '') In this D and g means

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.