servicenow

umar5
Tera Contributor

............................................

6 REPLIES 6

Ashish Parab
Mega Sage

Hello @umar5 ,

 

You can try the below onChange client script that converts Indian format (1234567890) to US fromat (123-456-7890).

 

Script -

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Remove any non-digit characters from the input
    var cleanNumber = newValue.replace(/\D/g, '');

    // Check if the cleaned number is exactly 10 digits
    if (/^\d{10}$/.test(cleanNumber)) {
        // Format the number into US format: 123-456-7890
        var formattedNumber = cleanNumber.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
        
        // Set the formatted number back to the field
        g_form.setValue('mobile_no', formattedNumber);
    } else {
        // Display an error message if the input is not valid
        g_form.addErrorMessage('Please enter a valid 10-digit mobile number.');
        g_form.clearValue('mobile_no'); 
    }
}

 

Output -

AshishParab_0-1736755588346.png

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

 

@umar5 ,

 

Have you tried using this provided script? If it doesn’t work, please let me know. I’ll be happy to assist further!

 

Thanks,

Ashish

Ankur Bawiskar
Tera Patron
Tera Patron

@umar5 

why?

what's your business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

umar5
Tera Contributor

@Ankur Bawiskar 

 

Hope you are doing well !

I have a string field called Phone number when user enters the phone number in indian format it should convert into US format. Can you please help me in this?

 

Thanks & Regards,

Umar Shaik.