I need to have value in USD.

niveditakumari
Mega Sage

Hi, 

 

I have created field in catalog item that is single line text and it should only take value in USD. 

Can anyone please help me to achieve that. 

 

Regards, 

Nivedita 

 

 

3 ACCEPTED SOLUTIONS

Sohail Ahmed
Tera Guru

Hi @niveditakumari 

Can you try using this on-change client script on the field 

function onChange(control, oldValue, newValue, isLoading) {
    // If the form is loading, don't validate
    if (isLoading) {
        return;
    }

    // The value of the field being validated
    var value = newValue;

    // Regular Expression to match the US currency format: $13,000.00 or 13,000.00
    var regex = /^\$?\d{1,3}(,\d{3})*(\.\d{2})?$/;

    // Test if the value matches the regex
    if (!regex.test(value)) {
        // Show an error message if the value does not match the pattern
        g_form.showFieldMsg('your_field_name_here', "Please enter a valid amount in USD format (e.g., $13,000.00).", "error");
        g_form.setValue('your_field_name_here', '');     }
}

- Thank you ,

Sohail Ahmed Syed

View solution in original post

sunil maddheshi
Tera Guru

@niveditakumari 

Hi You can follow below example:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Regular expression for USD format (e.g., $100.00, $1,000,000.99, $0.50)
    var usdRegex = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;

    if (!usdRegex.test(newValue)) {
        alert("Please enter a valid USD amount in the format: $1,234.56");
        g_form.clearValue('your_field_name'); // Clears incorrect input
    }
}

Please marl correct/helpful if this helps you!

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@niveditakumari 

try this without scripting using variable validation Regex and then attach to your variable

OR

use this onChange script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.hideFieldMsg('variableName');

    // Regular expression to match USD values (e.g., 1234.56 or 1234)
    var regExp = /^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/;

    if (!regExp.test(newValue)) {
        g_form.showFieldMsg('variableName', "Please enter a valid USD amount (e.g., 1234 or 1234.56).", "error");
        g_form.clearValue('variableName'); // variableName
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

14 REPLIES 14

Juhi Poddar
Kilo Patron

Hello @niveditakumari

Follow these steps: 

  • Navigate to All -> Variable Validation Regex

JuhiPoddar_0-1741773283063.png

  •  Create a New record and add Regular expression ^\$\d+(\.\d{2})?$ JuhiPoddar_2-1741773448047.png

     

  • Add this Regex to the variable in type specification sectionJuhiPoddar_3-1741773625454.png

Note: 

  • This ensures the field only accepts values starting with $
  • Now, users must enter values like $1000.00 or $1000 and incorrect formats (e.g., 1000.00 or 1000) will not be accepted.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Ankur Bawiskar
Tera Patron
Tera Patron

@niveditakumari 

try this without scripting using variable validation Regex and then attach to your variable

OR

use this onChange script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.hideFieldMsg('variableName');

    // Regular expression to match USD values (e.g., 1234.56 or 1234)
    var regExp = /^\$(\d{1, 3}(\, \d{3})*|(\d+))(\.\d{2})?$/;

    if (!regExp.test(newValue)) {
        g_form.showFieldMsg('variableName', "Please enter a valid USD amount (e.g., 1234 or 1234.56).", "error");
        g_form.clearValue('variableName'); // variableName
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@niveditakumari 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar

 

It is not taking any value. When I'm adding any value it is giving error. 

Please see attached screenshot : 

niveditakumari_0-1742216219003.png

 

Can you please correct that. 

 

Regards, 

Nivedita 

 

 

@niveditakumari 

you need to tweak it as per your requirement.

Few of the regex you can refer

Basic format (matches amounts like $100, $100.50):

\$\d+(\.\d{2})?

Including commas for thousands (matches amounts like $1,000, $1,000.50):

\$\d{1,3}(,\d{3})*(\.\d{2})?

Optional cents (matches amounts like $100, $100.50, $100.5):

\$\d+(\.\d{1,2})?

Allowing spaces (matches amounts like $ 100, $ 1,000.50):

\$\s?\d{1,3}(,\d{3})*(\.\d{2})?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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