0

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;
}

}

  • 1
    Please remove the tag `php`, it's not related with the code shared. For listening to event on `KEYPRESS` check this https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event – Rui Costa Jul 08 '21 at 18:09
  • 1
    Why are you checking `if(window.event)` and then only use `e.keyCode` inside? Inline event handlers like `onkeypress`, `onkeyup`, or `onchange` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. Also, `keypress` is deprecated. Consider using [`input`](//developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event) instead. – Sebastian Simon Jul 08 '21 at 18:10
  • Haven't you forget to copy the NumbersOnly function ? – Dani Jul 08 '21 at 18:19

1 Answers1

0

Since I've no idea what your functions are supposed to do, here's a working code snippet where the functions get called at least.

document.addEventListener('keypress', keyPressFunction);
document.addEventListener('keyup', formatPhone);
document.addEventListener('onchange', formatPhone);

function keyPressFunction(e) {
  NumbersOnly(e);
  formatPhone(e);
}

function NumbersOnly(e) {
  if (e.keyCode > 57 | e.keyCode < 48) {
    return false;
  }
}

function formatPhone(e) {
  alert();
  var keychar;
  if (e) {
    var keynum = e.keyCode;
    keychar = String.fromCharCode(keynum)
  }
  if (/[\b]/.exec(keychar)) {
    return true;
  } else {
    var p = e.target.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);
    }
    e.target.value = p;
    return false;
  }
}
<input type="text" name="cell_phone_txt" id="cell_phone_txt" maxlength="15" style="width:100px;" value="Hi" placeholder="Cell Phone Number">
Sana Mumtaz
  • 803
  • 7
  • 16