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

Hi @Sohail Ahmed

 

It is not showing field message. 

Please find attached screenshot : 

 

niveditakumari_0-1742122341487.pngniveditakumari_1-1742122368396.png

 When I'm adding here wrong value then It is showing field in red color and then field is becoming empty but not showing field error message. 

 

Can you please help me to correct that. 

 

Regards, 

Nivedita 

 

 

Hi @niveditakumari ,

Can you please try first clearing the field value and then showing the field message ? 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        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.setValue('field_name', '');
        g_form.showFieldMsg('field_name', "Please enter a valid amount in USD format (e.g., $13,000.00).", "error");

    }
}

 

SohailAhmed_0-1742128251969.png

 

 Thanks,

Sohail Ahmed Syed

Hi @Sohail Ahmed

 

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

Please see attached screenshot : 

niveditakumari_1-1742216341989.png

 

Can you please help me to correct that. 

 

Regards, 

Nivedita 

 

 

 

 

Hi @niveditakumari  ,

Were you using validation regex or client script ? Client script should work fine 

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!