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

simple regex to allow numbers from -20 to 20 with decimal points

how can I specify a regex that allows numbers from -20 to 20, including 0 and decimal points?

e.g all of the following are allowed:

20
19.99
5
0.5
0
1
3.45
-20
-17.20

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 :

If you’re looking for a regex for your task, you can use this one:

^-?1?\d(\.[\d]+)?$|^-?20$

Explanation:

  • ^: start of number
  • -?: optional minus
  • 1?: optional 1 for tens
  • \d: any digit for units
  • (\.[\d]+)?: optional decimal places with any digit
  • $: end of number

or

  • ^: start of number
  • -?: optional minus
  • 20: 20
  • $: end of number
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