The CreatorCon Call for Content is officially open! Get started here.

RegExp help in validating numbers in a string field

Suryansh Verma
Tera Contributor

Hello Community,

 

I have a string field for inputting hours.

 

The field should only allow negative numbers & decimal(.) & also comma(,) for that i am using the below expression.

/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:(\.|,)\d+)?$/

 

now I also want to restrict the decimal places (before and after ) to upto 3 digits only and also 

 

so accepted value should be 
-123.456 or -123,456 and not -1234.5678 or -1234,5678

1 ACCEPTED SOLUTION

Hemant Goldar
Mega Sage

Hi @Suryansh Verma,

 

Please try the below regular expression

 

^-?(?:\d{1,3}(?:,\d{3})*|\d+)([.,]\d{1,3})?$

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

View solution in original post

6 REPLIES 6

Hemant Goldar
Mega Sage

Hi @Suryansh Verma,

 

Please try the below regular expression

 

^-?(?:\d{1,3}(?:,\d{3})*|\d+)([.,]\d{1,3})?$

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

Thank you @Hemant Goldar for your prompt response.

 

It works perfectly for restricting numbers after decimal places.

Can we also restrict numbers before decimal places to 4 or max 5 digits?

Hi @Suryansh Verma,

 

Can you provide me the list of valid numbers that should accept and shouldn't be?

 

And please mark the previous comment as the correct answer to close the thread.

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

 

@Hemant Goldar 

 

so it should accept
-12345.897 or -12345,897 but not -1234555555555.897 or -1234555555555,897