OL Learn

Regex for currency

Need to have only "currency/intergers " in a string.
But sometimes the customer writes charecters int the mapped field.
Anybody know how to check for tha ?.
I have tried replaceALL and matches - but the designer does not allow that.
I simply need to show another text, it the current field does not match only a currency.

My script - Line 3 fails with the currencyNoSymbol when text are in the string.
var result = “”;
var Total = record.fields[“ValueCustoms”];
var Total = formatter.currencyNoSymbol(record.fields[“ValueCustoms”]);
if (Total !== “”) result += ‘’ + Total;
else result = ‘0,00’;
results.html(result);

Do something like this:

var Total = record.fields["ValueCustoms"];
var extra = Total.replace(/[0-9\.,\-]/gi,"");
if(extra){
   result = "0,00"
} else {
   result += "" + formatter.currencyNoSymbol(Total);
}

In that code we apply a regex to the extracted field value to create a variable named extra that contains any characters that are not digits, a comma, a period or a minus sign. So, if that extra variable contains anything, then it means the number was invalid to start with.

There are other ways of doing this ( Number.isNaN(parseFloat(Total)) for instance), but the regex allows you greater flexibility if you want to check for special notation, like negative numbers being encased between parentheses.

Thaks it is working perfectly