В настоящее время у меня есть несколько сценариев, которые принимают номера, которые пользователь входит в текстовое поле для номера телефона, и преобразует номер в стандартный (123) 456-7890 формата. При проверке моего скрипта (Code Beautify) он бросает ошибки, такие как EVT., которые и evt.keycode устарели, документ, используемый до того, как он был отклонен, и т. Д. Поле, он заставит его отобразить его в (123) 456-7890 формате как в самом поле, так и в выходе.
// Format the phone number as the user types it
document.getElementById('ContactPhone').addEventListener('keyup', function(evt) {
var ContactPhoneVal = document.getElementById('ContactPhone');
var charCode = (evt.which) ? evt.which : evt.keyCode;
ContactPhoneVal.value = phoneFormat(ContactPhoneVal.value);
});
// We need to manually format the phone number on page load
document.getElementById('ContactPhone').value = phoneFormat(document.getElementById('ContactPhone').value);
// A function to determine if the pressed key is an integer
function numberPressed(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 36 || charCode > 40)) {
return false;
}
return true;
}
// A function to format text to look like a phone number
function phoneFormat(input) {
// Strip all characters from the input except digits
input = input.replace(/\D/g, '');
// Trim the remaining input to ten characters, to preserve phone number format
input = input.substring(0, 10);
// Based upon the length of the string, we add formatting as necessary
var size = input.length;
if (size === 0) {
input = input;
} else if (size < 4) {
input = '(' + input;
} else if (size < 7) {
input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6);
} else {
input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6) + ' - ' + input.substring(6, 10);
}
return input;
}< /code>
В настоящее время у меня есть несколько сценариев, которые принимают номера, которые пользователь входит в текстовое поле для номера телефона, и преобразует номер в стандартный (123) 456-7890 формата. При проверке моего скрипта (Code Beautify) он бросает ошибки, такие как EVT., которые и evt.keycode устарели, документ, используемый до того, как он был отклонен, и т. Д. Поле, он заставит его отобразить его в (123) 456-7890 формате как в самом поле, так и в выходе.[code]// Format the phone number as the user types it document.getElementById('ContactPhone').addEventListener('keyup', function(evt) { var ContactPhoneVal = document.getElementById('ContactPhone'); var charCode = (evt.which) ? evt.which : evt.keyCode; ContactPhoneVal.value = phoneFormat(ContactPhoneVal.value); });
// We need to manually format the phone number on page load document.getElementById('ContactPhone').value = phoneFormat(document.getElementById('ContactPhone').value);
// A function to determine if the pressed key is an integer function numberPressed(evt) { var charCode = (evt.which) ? evt.which : evt.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 36 || charCode > 40)) { return false; } return true; }
// A function to format text to look like a phone number function phoneFormat(input) { // Strip all characters from the input except digits input = input.replace(/\D/g, '');
// Trim the remaining input to ten characters, to preserve phone number format input = input.substring(0, 10);
// Based upon the length of the string, we add formatting as necessary var size = input.length; if (size === 0) { input = input; } else if (size < 4) { input = '(' + input; } else if (size < 7) { input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6); } else { input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6) + ' - ' + input.substring(6, 10); } return input; }< /code> [/code]