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

sunil maddheshi
Tera Guru

@Jeck Manalo 

Please check below links should help you

validate a mobile number  

MOBILE NUMBER VALIDATION 

 

Please mark correct/helpful if this helps you!

@Jeck Manalo 

I have updated your code little bit, can you try once

function onSubmit() {
    var phoneNumber = g_form.getValue('phone_number');
    var mobileNumber = g_form.getValue('mobile_number');

    // Function to validate phone numbers asynchronously
    function validatePhoneNumber(number, callback) {
        if (!number) {
            callback(false); 
            return;
        }

        var ga = new GlideAjax('VSHUserPhoneTerritory'); // Script Include
        ga.addParam('sysparm_name', 'validatePhoneNumber'); // Method
        ga.addParam('fullPh', number);
        ga.getXMLAnswer(function(response) {
            var res = JSON.parse(response);
            callback(res.valid);
        });
    }

    return false; // Prevent form submission initially
    validatePhoneNumber(phoneNumber, function(phoneValid) {
        validatePhoneNumber(mobileNumber, function(mobileValid) {
            if (!phoneValid && !mobileValid) {
                g_form.addErrorMessage('Please enter at least one valid phone number.');
            } else {
                if (!phoneValid) {
                    g_form.clearValue('phone_number');
                }
                if (!mobileValid) {
                    g_form.clearValue('mobile_number');
                }
                g_form.submit();
            }
        });
    });

    return false; // Prevent immediate submission
}

Please mark correct/helpful if this helps you!

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeck Manalo 

don't use GlideAjax in onSubmit as synchronous calls are not supported in portal.

Why not use onChange and validate it and throw error when number is invalid?

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

hi ankur,

 

I already have it in onchange, what i want is that 

if the value for both phone_number && mobile_number is invalid = avoid submitting the request.

if one of the value has a valid value = submit the request and clear the value that has invalid.

 

Which I am trying to do.

 

JeckManalo_0-1741066201422.png