How to validate if the input is a valid phone number only denied it if its a mobile phone

Jeck Manalo
Tera Guru

I am using the GlideElementPhoneNumber since its an OOTB to validate if phone is valid.

 

My question is how can we validate if the user input a mobile phone number format into business phone and make it as invalid ?

 

this is a business phone and its ok

 

 

JeckManalo_0-1744272525007.png

 

 

this is a mobile phone and still shows a valid phone

 

JeckManalo_1-1744272525010.png

 

 

my thinking is i will use a field in a user field then input the format for Business Phone and validate it if its written the same in business phone field then its valid instead of using OOTB.

 

2 ACCEPTED SOLUTIONS

Hi @Jeck Manalo,
Please create OnChange Catalog Client Script for your business phone field and use below script:

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

    var phone = newValue.trim();
    var validBusinessFormat = /^\+63\s2\s8866\s\d{4}$/;

    if (!validBusinessFormat.test(phone)) {
        g_form.showFieldMsg('phone', 'Invalid business phone format. Expected format: +63 2 8866 XXXX', 'error');
    } else {
        g_form.hideFieldMsg('phone', true);
    }
}

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

@Jeck Manalo 

Thank you for marking my response as helpful.

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

View solution in original post

6 REPLIES 6

Medi C
Giga Sage

Hello @Jeck Manalo,
The GlideElementPhoneNumber is helpful for basic phone number validation, but it doesn't distinguish between types of phone numbers (e.g., business vs. mobile) out of the box. 
You can use regular expressions to validate business phones. Please share how business phones are looking like and formatted on your side, I can help with regular expression validation.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

usually its written with this country code + area code (location).

 

  • +63: Country code for the Philippines
  • 2: Area code for Metro Manila
  • 8866 : Local subscriber number
  • then last 4 digits for the number of the user.

Hi @Jeck Manalo,
Please create OnChange Catalog Client Script for your business phone field and use below script:

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

    var phone = newValue.trim();
    var validBusinessFormat = /^\+63\s2\s8866\s\d{4}$/;

    if (!validBusinessFormat.test(phone)) {
        g_form.showFieldMsg('phone', 'Invalid business phone format. Expected format: +63 2 8866 XXXX', 'error');
    } else {
        g_form.hideFieldMsg('phone', true);
    }
}

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeck Manalo 

it's better to have your own logic and regular expression for validating rather than using the OOTB one

Something like this in onChange client script

var phoneNumber = g_form.getValue('phone_number'); // Replace 'phone_number' with your field name

    // Define the regex pattern for the phone number
    var pattern = /^\+63 2 8866 \d{4}$/;

    // Validate the phone number
    if (!pattern.test(phoneNumber)) {
        g_form.showFieldMsg('phone_number', 'Invalid phone number format. Expected format: +63 2 8866 XXXX', 'error');
        g_form.clearValue('phone_number'); // Clear the field if invalid
    } else {
        g_form.hideFieldMsg('phone_number');
    }

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