Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular Expression for Negative & Positive Number Except Zero (0) Upto two (2) Decimal

What would be asp .net regular expression for allowing negative & positive number except 0 with only up-to 2 decimal places? It should not allow more than 2 digits after decimal. So allowed would be

1
72.25
568
7898578
-1
-3233443
0.01
-0.14

Disallowed

0
1.235
-5.658
0.0
0.00

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:

https://regex101.com/r/tbdE5Q/latest

^(?!0+(\.0+)?)-?\d+(?:\.\d{1,2})?$

Explaination

^ anchors start of string


(?! ensures that the match is not …

0+ constituted of only 0

(\.0+)? and optionally a decimal point . followed by only 0s

) ends 0 case


-? allows - sign

\d+ any number of digits before decimal point

(?: optional non-capture group

\. decimal point …

\d{1,2} and up to two digits

)? end optional non-capturing group


$ anchors end of string

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading