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.
Description:
In previous articles I explained jQuery disable f12 or console window in browser example, jQuery difference between == and === with example, jQuery show loading image on button click with example, jQuery get radio button selected value by name, jQuery validate radiobuttonlist with example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain how to convert number to phone number format in JavaScript with example.
In previous articles I explained jQuery disable f12 or console window in browser example, jQuery difference between == and === with example, jQuery show loading image on button click with example, jQuery get radio button selected value by name, jQuery validate radiobuttonlist with example and many articles relating to css, AngularJS, jQuery, JavaScript and asp.net. Now I will explain how to convert number to phone number format in JavaScript with example.
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. |
|||
|
|||
1 comments :
phnumber.replace(/\D/g, '') In this D and g means
Note: Only a member of this blog may post a comment.