How to validate string field to currency type.

sanjana5
Tera Contributor

How to validate string field to currency type.

@BharathChintala

1 ACCEPTED SOLUTION

BharathChintala
Mega Sage

@sanjana5  You can use Regex to achieve this

 

Write a OnChange client script  on string field change

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var regex = /(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$/;
    if (!g_form.getValue('amount').match(regex)) {// replace string field backend name
        alert('Invalid entry! Please use only numbers and/or decimals in your amount value.');
        g_form.clearValue('amount');// replace string field backend name
    } else {
        var num = g_form.getValue("amount");// replace string field backend name
        var currencyVal = num.replace(/(\d)(?=(\d{3})+(?!\d))/g'$1,');
        g_form.setValue('amount', currencyVal);
    }
}
 
 
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

3 REPLIES 3

BharathChintala
Mega Sage

@sanjana5  You can use Regex to achieve this

 

Write a OnChange client script  on string field change

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var regex = /(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$/;
    if (!g_form.getValue('amount').match(regex)) {// replace string field backend name
        alert('Invalid entry! Please use only numbers and/or decimals in your amount value.');
        g_form.clearValue('amount');// replace string field backend name
    } else {
        var num = g_form.getValue("amount");// replace string field backend name
        var currencyVal = num.replace(/(\d)(?=(\d{3})+(?!\d))/g'$1,');
        g_form.setValue('amount', currencyVal);
    }
}
 
 
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Working as Expected Thank you @BharathChintala  : )

Hey,

 

Thanks for providing the code, however, when I use this on the cmdb_ci_business_app table for one of string fields, I get the below error message:

"onChange script error: RangeError: Maximum call stack size exceeded function () { [native code] }"

 

Any thoughts?