Create Custom Regex For Currency

jmiskey
Kilo Sage

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

1 ACCEPTED SOLUTION

DGAJ
Mega Guru

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

 

DGAJ

View solution in original post

6 REPLIES 6

Rafael Batistot
Kilo Patron

Hi @jmiskey 

May you try 

^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$

 

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:

jmiskey_0-1753900040266.png

 

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.

DGAJ
Mega Guru

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

 

DGAJ