Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Help with regex to strip off a dollar sign from a field on service portal form

Bruler1230
Tera Expert

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

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

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.

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please use onChange client script on that variable

Also share which values are allowed and which are restricted

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

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

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

asifnoor
Kilo Patron

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.

Willem
Giga Sage
Giga Sage

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