I am having trouble debugging why the formatPhone() function is not getting called with the following input tag (identical code works correctly in other scripts--I have copied and pasted the working code from other scripts to the one that is failing to no avail). These are existing functions that I did not write and they are used successfully throughout the systems I maintain. Any assistance would be most helpful.
HTML:
<input type="text"
name="cell_phone_txt"
id="cell_phone_txt"
maxlength="15"
style="width:100px;"
onkeypress="NumbersOnly(); return formatPhone(this, event);"
onkeyup="formatPhone(this);"
onchange="formatPhone(this);"
value="<?php echo $cell_phone_txt ?>"
placeholder="Cell Phone Number"
>
function NumbersOnly()
{
if (window.event.keyCode > 57 | window.event.keyCode < 48)
{
window.event.returnValue=false
}
}
function formatPhone(elm, e)
{
alert();
var keychar;
if (e)
{
var keynum;
if (window.event)
{
keynum = e.keyCode
}
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
}
if (/[\b]/.exec(keychar))
{
return true;
}
else
{
var p = elm.value + keychar;
p = p.replace(/^[01]/,"");
p = p.replace(/\D+/g, "");
if (p.length > 0 && p.length < 3)
{
p = "("+p;
}
else if (p.length >= 3 && p.length < 7)
{
p = "("+p.substring(0,3)+") "+p.substring(3);
}
else if (p.length >= 7 && p.length < 10)
{
p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
}
else if (p.length)
{
p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
}
elm.value = p;
return false;
}
}