Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/23/2021 in all areas

  1. Hi @jbloome and @Meekeee We can also use arrays and loops to further reduce the code: <script> const fields = ['InsertRecordPhone1','InsertRecordPhone2', 'InsertRecordPhone3']; const inputs = []; for (let f of fields){ inputs.push(document.querySelector('#' + f)); } for(let i of inputs){ i.maxLength = 14; i.onkeyup = telephize; i.onkeydown = telephize; } function telephize(v_e) { // this.value = this.value.replace( /\D+/g, "" ).replace( /([0-9]{1,3})([0-9]{3})([0-9]{4}$)/gi, "($1) $2-$3" ); //mask numbers (xxx) xxx-xxxx v_e = v_e || window.event; if (v_e.keyCode >= 65 && v_e.keyCode <= 90) { this.value = this.value.substr(0, this.value.length - 1); return false; } else if (v_e.keyCode >= 37 && v_e.keyCode <= 40) { return true; } var v_value = (this.value.replace(/[^\d]/g, '')); if (v_value.length == 7) { this.value = (v_value.substring(0, 3) + "-" + v_value.substring(3, 7)); } else if (v_value.length == 10) { this.value = ("(" + v_value.substring(0, 3) + ") " + v_value.substring(3, 6) + "-" + v_value.substring(6, 10)); }; } document.querySelector('#caspioform').onsubmit = function(e) { e.preventDefault(); if (input.value.length >= 14){ this.submit(); } else { alert('Please input a valid phone number'); input.focus(); } } </script>
    1 point
×
×
  • Create New...