TheConeHead
Programmer
Any suggestions for having phone numbers formatyted the same (ie (xxx) xxx-xxxx) with extensions being a possibility - I'd rather to a jscript or server-side solution as opposed to having sepearte form elements....
![[conehead] [conehead] [conehead]](/data/assets/smilies/conehead.gif)
![[conehead] [conehead] [conehead]](/data/assets/smilies/conehead.gif)
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function doFilter(o) {
var p = o.value;
p = p.replace(/[^0-9]/g, "");
if ( p.length >= 6 )
o.value = "(" + p.substr(0, 3) + ") " + p.substr(3, 3) + "-" + p.substr(6, 4);
else if ( p.length >= 3 )
o.value = "(" + p.substr(0, 3) + ") " + p.substr(3, 3);
}
//--></script>
</head>
<body>
<form>
<input type="text" onkeyup="doFilter(this);" />
</form>
</body>
</html>
function doFilter(o) {
var p = o.value;
p = p.replace(/[^0-9]/g, "");
if ( p.length >= 7 )
o.value = p.substr( 0, 1 ) + " (" + p.substr(1, 3) + ") " + p.substr(4, 3) + "-" + p.substr(7, 4);
else if ( p.length >= 4 )
o.value = p.substr( 0, 1 ) + " (" + p.substr(1, 3) + ") " + p.substr(4, 3);
else if ( p.length == 1 )
o.value = p.substr( 0, 1 ) + " (";
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function doFilter(e) {
if (!e) var e = window.event;
if (e.target) var o = e.target;
else if (e.srcElement) var o = e.srcElement;
if (o.nodeType == 3) // defeat Safari bug
o = o.parentNode;
if (e.keyCode != 8) {
var p = o.value;
p = p.replace(/[^0-9]/g, "");
if ( p.length >= 7 )
o.value = p.substr( 0, 1 ) + " (" + p.substr(1, 3) + ") " + p.substr(4, 3) + "-" + p.substr(7, 4);
else if ( p.length >= 4 )
o.value = p.substr( 0, 1 ) + " (" + p.substr(1, 3) + ") " + p.substr(4, 3);
else if ( p.length == 1 )
o.value = p.substr( 0, 1 ) + " (";
}
}
//--></script>
</head>
<body>
<form>
<label for="t1">
Enter phone number including country and area code:
<input name="t1" type="text" onkeyup="doFilter(event);" />
</label>
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Phone number formatter</title>
<script type="text/javascript">
<!--
function doFilter(e,o) {
var key = window.event ? e.keyCode : e.which;
var p = o.value;
if (key == 8)
p = (p.length == 5)? p.substring(0,p.length-2) : (p.length == 9)? p.substring(0,p.length-1) : p;
p = p.replace(/[^0-9]/g, "");
for (var x=0;x<p.length;x++) { //Do not allow areacode to start with a 1.
if (p.charAt(0) == 1)
p = p.substring(1);
}
for (var x=0;x<p.length;x++) { //Do not allow local exchange to start with a 1.
if (p.charAt(3) == 1)
p = p.substring(0,3) + p.substring(4);
}
if ( p.length >= 6 )
o.value = "(" + p.substr(0, 3) + ") " + p.substr(3, 3) + "-" + p.substr(6, 4);
else if ( p.length >= 3 )
o.value = "(" + p.substr(0, 3) + ") " + p.substr(3, 3);
else
o.value = p;
}
//-->
</script>
</head>
<body>
<form>
<input type="text" value="" onkeyup="doFilter(event,this);" />
</form>
</body>
</html>