How to validate a mobile number in a ServiceNow catalog form before submission?

kusumgautam
Tera Contributor

I have a Service Catalog form with a single-line text variable for capturing the mobile number. Before the user submits the form, I want to validate whether the entered mobile number exists in the sys_user table.

 If the mobile number exists, allow form submission.
If the mobile number does not exist, prevent submission and show an error message.

What is the best approach to achieve this validation on the catalog form

Thanks in advance for your help!

1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hi @kusumgautam ..

You can achieve this validation using a Catalog Client Script with an onSubmit event. This script will check the entered mobile number against the sys_user table using a GlideAjax call.
Sample script :-

create a script include with some name and make it client callable :-

var xxx= Class.create(); //xxx is script include name so choose your name accordinly
xxx.prototype = {
initialize: function() {},

yyy: function(mobileNumber) {
var userGR = new GlideRecord('sys_user');
userGR.addQuery('mobile_phone', mobileNumber);
userGR.query();
return userGR.hasNext(); // Returns true if mobile number exists, false otherwise
},

type: 'xxx'
};

 

Client Script :-

function onSubmit() {
var mobileNumber = g_form.getValue('mobile_number'); // Replace 'mobile_number' with the actual column name

if (!mobileNumber) {
g_form.clearMessages();
g_form.addErrorMessage("Please enter a mobile number.");
return false;
}

var ga = new GlideAjax('ValidateMobileNumber');
ga.addParam('sysparm_name', 'yyy');
ga.addParam('sysparm_mobile', mobileNumber);

var isValid = false;

ga.getXMLAnswer(function(response) {
isValid = response === 'true';

if (!isValid) {
g_form.clearMessages();
g_form.addErrorMessage("The entered mobile number does not exist in our records.");
return false;
} else {
return true;
}
});

return false; 
}

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

7 REPLIES 7

Bert_c1
Kilo Patron

You can start here:

 

field-administration/concept/c_UsePhoneNumberFields.html

 

Good luck on achieving your goal.

jMarshal
Mega Sage
Mega Sage

Hi @kusumgautam - @Bert_c1 is correct, it would be prudent to use a phone number field variable in your catalog form, to ensure it is properly formatted. You will have a lot of difficulty with this solution, if you have improperly formatted phone number information. You can then have an onsubmit client script which uses glideajax to sys_user with a query on user.mobile_phone to the value on the form. When no values return, cancel the form submission and display an appropriate user message.

jcmings
Mega Sage

Unfortunately your ask isn't as simple as it sounds... it involves a few things:

 

Regex on the variable

I don't believe there is a Phone number-type variable already. So for your single-line text variable, under the Type specifications tab of the variable record, you'll see a field called Validation regex. Here is where you can prevent non-phone numbers from being entered. I believe you'll need to create a new record on the question_regex table for your specific phone-number requirements. You can use ChatGPT to help you write the regex expression for your specific formatting.

 

GlideAjax, Script Include, client script

You'll also need to set up an onSubmit client script (which fires the GlideAjax) and allows/prevents submission, the GlideAjax itself, and a Script Include. But you may run into difficulty with the formatting of your phone numbers; you'll likely need to convert the value entered in the catalog variable to match the ph_number (back-end column) type. As for the GlideAjax and Script Include part, if you need help getting started, here's a post from a few weeks back that will help.