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

Amit Verma
Kilo Patron
Kilo Patron

Hi @kusumgautam 

 

The mobile number entered will be of requestor or it is for another user? If it is for requestor, you should not let the user enter the mobile number. Instead, as per the requestor, you should populate the mobile number On-Load of the catalog item.

 

For this requirement, you need to make use of a Script Include and an On-Load Catalog Client Script or you can create Mobile Number field as a reference field to sys_user table and make use of Auto-Populate feature to populate mobile number based on the catalog item requestor field.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

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/

Vaibhav_Nikam
Tera Guru

Hi @kusumgautam ,

you can use below script for onChange client script:

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

var pattern = /^(\+)([1-9]{2})(\s)(\d{10})$/g;
if(!pattern.test(newValue)){
	g_form.clearValue('u_contact_number');

alert('Phone enter a valid phone number');
}}  

 


If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.

Thanks,

Vaibhav Nikam