- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 10:56 AM
I am creating a Catalog Item. One of the Variables is asking for a dollar amount. So I have to use the Single Line Text variable type, as there is nothing else you can use to record this amount. I want to do some validation on this to ensure that they are entering a valid dollar amounts. The allowable characters should be:
$ (dollar sign)
, (comma)
. (period)
0-9 (numbers 0 through 9)
The dollar sign is not required, but if they do use it, it should be the first character only.
It should allow two numbers after the decimal, but decimals are not always required.
So here are some examples of valid entries:
$10,797.14
6,143.12
3,333
68
$111
$154.13
$5,000
The built-in ServiceNow Regex "Number" only allows numbers, and no other characters. So it will not work on the values I show above (except for 68). Can anyone help me to come up with the Custom Regex calculation that would do want we want?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 01:58 PM
Hi @jmiskey
Supposed your variable name is Cost"cost" - single line text.
Create an OnChange Catalog script: Price Format
and paste the code . I have added error handling as extra but you can comment out if not required.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue.trim() === '') {
return;
}
var cost = newValue.trim();
// Regular expression to validate dollar amount format
var dollarAmountRegex = /^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/;
if (!dollarAmountRegex.test(cost)) {
alert("Please enter a valid dollar amount (e.g., $1,234.56 or 1234.56)");
g_form.setValue('cost', oldValue);
return;
}
}
Added screenshots for reference. Hope it helps.
If this solved the issue, please mark my answer as helpful.
Thanks,
DJumah
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 11:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 11:27 AM
Did you try it out on your end first?
I tried it, and it doesn't work on ANY of the entries. Everything was rejected.
Here is how I set it up:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 12:42 PM
If it is too complicated to do what we want, I would just be happy for an expression that limits data entry to numbers, commas, periods, and dollar signs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2025 01:58 PM
Hi @jmiskey
Supposed your variable name is Cost"cost" - single line text.
Create an OnChange Catalog script: Price Format
and paste the code . I have added error handling as extra but you can comment out if not required.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue.trim() === '') {
return;
}
var cost = newValue.trim();
// Regular expression to validate dollar amount format
var dollarAmountRegex = /^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/;
if (!dollarAmountRegex.test(cost)) {
alert("Please enter a valid dollar amount (e.g., $1,234.56 or 1234.56)");
g_form.setValue('cost', oldValue);
return;
}
}
Added screenshots for reference. Hope it helps.
If this solved the issue, please mark my answer as helpful.
Thanks,
DJumah