Greetings,
I discovered the following code on how to format a phone number by placing it in the footer section of a data-page. However, I tried to use this code and tweak it to allow formatting for another field in same data-page where I enter a numeric value like 5654-58-8569. And so, I'm trying to edit this script so I can use if as follows: xxxx-xx-xxxx but can't seem to make it work.
So I have two questions if I may:
Can I edit the following code to accomplish the above, and ...
Can I use both codes separately in the same footer (one for the phone field, the second for the number field as noted)
Here's the code I found (which works perfectly for the phone field):
<script>
var field = 'InsertRecordgpin';
var input = document.querySelector('#' + field);
input.maxLength = 14;
input.onkeyup = telephize
input.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.addEventListener('BeforeFormSubmit', function(e) {
if (input.value.length < 14) {
e.preventDefault();
alert('Please input a valid phone number');
input.focus();
}
});
</script>