- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-13-2022 08:22 PM
HI Experts,
Kindly help me, I have a mobile number variable in Single line type.
If entered as 0420111111, it should format as +61 420 111 111 and should happen onChange action.
i tried creating an Onchange catalog client script with below code i found but it is only accepting 10 digits and would not display the correct format. ANY HELP WOULD BE APPRECIATED. THANK YOU!
var pattern = /^\(\d{3}\)\s\d{3}-\d{4}$/; //(xxx) xxx-xxxx
var phone = g_form.getValue('u_mobile_phone');
if (!pattern.test(phone)) {
phone = phone.replace(/\D/g, '');
var regex = /^\d{10}$/;
var is_valid = regex.test(phone);
if (!is_valid) {
g_form.clearValue('u_mobile_phone');
alert('Invalid phone number. Please enter 10 digits');
} else {
phone = '(' + phone.slice(0, 3) + ') ' + phone.slice(3, 6) + '-' + phone.slice(6, 10);
g_form.setValue('u_mobile_phone', phone);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 06:05 AM
Hi, Try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var pattern = /^\+\d{2}(?:\s\d{3}){3}$/;
if(!pattern.test(newValue))
{
var length = g_form.getValue('u_mobile_phone').length;
if(length != 10)
{
g_form.clearValue('u_mobile_phone');
g_form.showErrorBox('u_mobile_phone', 'enter 10 digits only');
}
else
{
g_form.setValue('u_mobile_phone', '+61 ' + newValue.slice(1,4) + ' ' + newValue.slice(4,7) + ' ' + newValue.slice(7));
}
}
}
Example input : 0420111111
Example Output : +61 420 111 111
Example input : 0420111
Example Output : Enter 10 digits error
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 05:50 AM
Hey,
Please try below script:
var phone= g_form.getValue('u_mobile_phone');
var check = /^[0-9]{10}/;
var verify = phone.match(check);
if(verify == "true"){
var res = phone.slice(1);
g_form.setValue('u_mobile_phone', '+61 ' + res.slice(0,3) + ' ' + res.slice(3,6) + ' ' + res.slice(6) );
}
else{
g_form.clearValue('u_mobile_phone');
alert('Invalid phone number. Please enter 10 digits');
}
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 06:05 AM
Hi Aman,
I tried this code but it is giving me the alert "Invalid phone number. Please enter 10 digits". Although i have tried several times inputting 10 digits. š

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 06:05 AM
Hi, Try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var pattern = /^\+\d{2}(?:\s\d{3}){3}$/;
if(!pattern.test(newValue))
{
var length = g_form.getValue('u_mobile_phone').length;
if(length != 10)
{
g_form.clearValue('u_mobile_phone');
g_form.showErrorBox('u_mobile_phone', 'enter 10 digits only');
}
else
{
g_form.setValue('u_mobile_phone', '+61 ' + newValue.slice(1,4) + ' ' + newValue.slice(4,7) + ' ' + newValue.slice(7));
}
}
}
Example input : 0420111111
Example Output : +61 420 111 111
Example input : 0420111
Example Output : Enter 10 digits error
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 06:11 AM
Thank you, Sumanth! this is working for me! š

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-14-2022 06:21 AM