- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 03:38 AM
Hello,
I have a form on the service portal that is for users to enter a cost. However, I do not want users to add a dollar sign($) to the amount entered. So I want to add a regex that will strip off the dollar sign if entered. Additionally, i only want users to be able to enter numbers and commas into this field. I know this can be done in a catalog client script, or by creating a new regex in the variable validation regex module. I know this probably isnt too hard but i am not familiar with regular expressions. Any help is appreciated.
thanks
Solved! Go to Solution.
- Labels:
-
Multiple Versions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 03:57 AM
Hi
Here is the regex you can use which allows only numbers and comma.
^[0-9,\,]*$
Kindly mark the comment as a correct answer and helpful if this answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 03:49 AM
Hi,
please use onChange client script on that variable
Also share which values are allowed and which are restricted
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 03:55 AM
Hi,
Something like this
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
newValue = newValue.replace("$",""); // to remove dollar sign
var regexp = /^[0-9.,]+$/;
if(!regexp.test(newValue))
{
alert('Only numbers and commas are allowed');
g_form.clearValue('variableName');
}
//Type appropriate comment here, and begin script below
}
Validation Regex: then use this in your variable
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 03:57 AM
Hi
Here is the regex you can use which allows only numbers and comma.
^[0-9,\,]*$
Kindly mark the comment as a correct answer and helpful if this answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2020 04:05 AM
If you want to check for valid price like entries use:
/^(\d*([.,](?=\d{3}))?\d+)+((?!\2)[.,]\d\d)?$/
Will validate only if in correct format, with dots or commas in their respective places.
International format for the en_US locale & US national format:
- 134.56
- 1,234.56
- 2,991,234.00
Italian national format with 2 decimals:
- 134,56
- 21.234,56
- 1.234,56
- 9.321.234,56
International format for the de_DE locale:
- 134,56
- 1234,56
- 98281234,56
Decimals are optional, validation for whole amounts:
- 1234
- 1,234
- 2,991,234
- 1.234
- 9.321.234
Like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
newValue = newValue.replace("$", ""); // to remove dollar sign
var regexp = /^(\d*([.,](?=\d{3}))?\d+)+((?!\2)[.,]\d\d)?$/;
if (!regexp.test(newValue)) {
g_form.showFieldMsg('variableName', 'Value should be like 0.0 or 0,0', "error");;
g_form.clearValue('variableName');
}
}
Refer to:
https://stackoverflow.com/questions/15958808/regexp-for-validating-price