Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
You can use this javascript for phone number format while telephone is being entered into a textbox or mask textboxes for phone numbers.
This free javascript phone format code is prepared to format phone numbers in "(123) 456-7890" phone format.
You are free to use this javascript phone number format code.
Phone Format as "(123) 456-7890"
<script language="javascript">
<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->
<!-- Format : "(123) 456-7890" -->
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;
function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}
function backspacerUP(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if(keycode >= 48){
ValidatePhone(object)
}
}
function backspacerDOWN(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function GetCursorPosition(){
var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
window.status=cursorposition
bool=true
}
}
}
}
The modification in this article aims to format phone numbers like "(123) 456-7890" that has a " " (space) character in it.
This additional space character differs this javascript phone formatting code from its original version.
The original javascript code is used for auto formatting telephone numbers by using a mask like "(123)456-7890" which has no space in it.