Issue with US Phone Number Formatting in Record Producer (onChange Client Script)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi,
I have a Phone Number variable in a Record Producer. My requirement is to automatically format the entered phone number into the US format:
+1 (XXX) XXX-XXXX
I wrote the following onChange Client Script:
// Remove all non-digit characters
var digits = newValue.replace(/\D/g, '');
// If only 10 digits are entered, assume US number
if (digits.length == 10) {
var formatted = '+1 (' +
digits.substring(0, 3) + ') ' +
digits.substring(3, 6) + '-' +
digits.substring(6);
g_form.setValue('u_employee_aided_contact_telephone_number', formatted);
g_form.hideFieldMsg('u_employee_aided_contact_telephone_number', true);
} else if (digits.length > 0) {
g_form.showFieldMsg(
'u_employee_aided_contact_telephone_number',
'Please enter exactly 10 digits.',
'error'
);
}The script successfully:
- Validates that the phone number contains exactly 10 digits.
- Automatically formats it as +1 (XXX) XXX-XXXX.
- Displays an error message when the input is invalid.
Issue I'm facing:
Once the number is formatted, editing it becomes difficult.
For example:
- I enter a 10-digit number.
- I realize that the 2nd and 3rd digits are incorrect.
- I delete those two digits and try to enter the correct digits.
- The field does not allow me to type the new digits.
Similarly, if I delete any digit from the middle of the formatted phone number and try to enter a replacement, the input is not accepted.
It seems that after g_form.setValue() formats the value, the field becomes difficult to edit because the onChange script keeps re-triggering.
Has anyone encountered this issue before? What is the recommended approach to allow users to edit the formatted phone number while still enforcing the US phone number format?
Any suggestions or best practices would be appreciated.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @mania,
I was able to reproduce the same
function onChange(control, oldValue, newValue, isLoading) {
// Skip during form load
if (isLoading) {
return;
}
// If the field is empty, clear any error messages
if (!newValue || newValue.trim() === '') {
g_form.hideFieldMsg('employee_phone', true);
return;
}
// Check if the value is already properly formatted
// If it matches the US format, skip processing
if (newValue.match(/^\+1 \(\d{3}\) \d{3}-\d{4}$/)) {
g_form.hideFieldMsg('employee_phone', true);
return;
}
// Remove all non-digit characters
var digits = newValue.replace(/\D/g, '');
// Only process if we have exactly 10 digits
if (digits.length === 10) {
// Format the number
var formatted = '+1 (' +
digits.substring(0, 3) + ') ' +
digits.substring(3, 6) + '-' +
digits.substring(6);
// Set the formatted value
g_form.setValue('employee_phone', formatted);
g_form.hideFieldMsg('employee_phone', true);
} else if (digits.length > 0 && digits.length < 10) {
// Show error for incomplete numbers
g_form.showFieldMsg(
'employee_phone',
'Please enter exactly 10 digits.',
'error'
);
} else if (digits.length > 10) {
// Show error if more than 10 digits
g_form.showFieldMsg(
'employee_phone',
'Please enter exactly 10 digits (no more than 10).',
'error'
);
}
}
If users need to edit the phone number frequently, consider performing the formatting in an onSubmit Catalog Client Script or on the server side after submission, rather than formatting it on every onChange event.