- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2022 04:26 AM
Hi All,
I am using the below code to make a variable accept only numeric value but it it not working.
Please check and suggest how we can make a field accept numeric values only.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var regexp = /^[+]?\d*$/;
if(!regexp.test(newValue)) {
alert('Please enter numeric value');
g_form.setValue('provide_approximate_yearly_spend','');
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2022 04:36 AM
Hello,
No need for client script, you can use Regex method, please go through articles
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2022 04:36 AM
Hello,
No need for client script, you can use Regex method, please go through articles
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2022 04:46 AM
Hello @AKASH BANERJEE
No need to write code as OOTB ServiceNow has regex defined for numeric value. Open your variable definition, go to "Type Specification" section in the form and select validation regex to number. Example:
Thank you,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2022 05:47 AM - edited ‎10-25-2022 05:48 AM
Hi @AKASH BANERJEE ,
You can use validation regex from variable , or you can write a client script for the variable .
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var phonenumber = g_form.getValue('phone_number'); // enter your variable name
if (isNaN(phonenumber)) { //isNAN Checks whether the entered value is number or not, isNAN = Not A Number
g_form.clearValue('phone_number');
g_form.showFieldMsg('phone_number', 'Only numbers are allowed in the phone number field, please use no spaces or any other characters when adding phone number', 'error');
}
}
Thanks,
Sohail