Validate Phone Number using OnSubmit

Jeck Manalo
Tera Guru
Hello Everyone,
 
I am trying to validate if phone number is valid or not using OnSubmit.
Unfortunately when phone_number or mobile_number has a valid phone number it still not be able to submit the record.
 
Any idea how to achieve this?
 
here is the sample code.
 
 
function onSubmit() {
    var phoneNumber = g_form.getValue('phone_number');
    var mobileNumber = g_form.getValue('mobile_number');

    // Replace hyphens with spaces in phone number and mobile number
    phoneNumber = phoneNumber.replace(/-/g, ' ');
    mobileNumber = mobileNumber.replace(/-/g, ' ');

    // Function to validate phone numbers
    function validatePhoneNumber(number, callback) {
        var ga = new GlideAjax('VSHUserPhoneTerritory'); // Script include
        ga.addParam('sysparm_name', 'validatePhoneNumber'); // Method
        ga.addParam('fullPh', number); // Parameters
        ga.getXMLAnswer(function(response) {
            var res = JSON.parse(response);
            callback(res.valid);
        });
    }


// Validate both phone numbers
validatePhoneNumber(phoneNumber, function(phoneValid) {
validatePhoneNumber(mobileNumber, function(mobileValid) {
   
// Assuming this is inside a function
if (!phoneValid && !mobileValid) {
    g_form.addErrorMessage('Please input a valid business phone number or mobile phone number');
    return false; // Prevent form submission
}

// The following code will only execute if at least one of the numbers is valid
if (phoneValid && !mobileValid) {
    g_form.clearValue('mobile_number');
    return true; // Allow form submission
}

if (!phoneValid && mobileValid) {
    g_form.clearValue('phone_number');
    g_form.clearValue('pvn');
    return true; // Allow form submission
}

});
});
// If neither condition is met, prevent form submission
 return false;
       
}
1 ACCEPTED SOLUTION

@Jeck Manalo 

you can have a hidden text variable and store the flag

1) set the flag as valid in both the onchange catalog client scripts if it's valid number

2) set it as invalid if any 1 number is not valid

3) then use onSubmit and check what's the flag value and if it's valid then allow submitting

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

8 REPLIES 8

even if the value is invalid it still submit the record

JeckManalo_1-1741066741424.png

 

@Jeck Manalo 

Another approach is don't use GlideAjax in your onSubmit script because of the restrictions I mentioned i.e. synchronous Ajax is not allowed in portal

Why not directly use regex in client side?

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

@Jeck Manalo 

you can have a hidden text variable and store the flag

1) set the flag as valid in both the onchange catalog client scripts if it's valid number

2) set it as invalid if any 1 number is not valid

3) then use onSubmit and check what's the flag value and if it's valid then allow submitting

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

thank you ankur this tips is works.