Variable for decimal and commas combination

Prati
Tera Contributor

Hello Everyone,

I need to have variable on my record producer form which is will only allow to enter the numbers values in he decimal and comma format.

we are able to achieve this but the thing is once value is entered and then again try to enter few more numbers it is not taking the format.

for example: if i enter amount as 198.99,000 then in same if i enter extra numbers 198.99,000899090like this it is not taking commas.

Can anyone help me on this. we tried to achieve this on change client script.

Thanks you

4 REPLIES 4

OlaN
Giga Sage
Giga Sage

Hi,

Are you using some kind of regular expression to validate the field?

If so, an expression like this one will allow only to enter numbers, commas and dots, and you can use it to test if the entered value match, and if not give an error message and clear the field.

var regex = /[\d,.]/;

Prati
Tera Contributor

we are using  like var reg = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:(\.|,)\d+)?$/;

That seems like a very complicated regex.

If you want to ensure numbers, followed by an optional comma and some more digits, you could simplify it, something like this.

var regex = /^-?[\d]+([,][\d]{1,4})?$/;

Can you share your client script for also?

Harish KM
Kilo Patron
Kilo Patron

HI you can do like this. Try in bg script

var num = 1234567.89;
var commas = num.toLocaleString();
var commas = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); // will add comma after 3 digits
gs.info(commas);

 

https://code-boxx.com/add-comma-to-numbers-javascript/

Regards
Harish