admin管理员组

文章数量:1399297

i been searching the net for a while without any success. i appreciate if anyone can solve this problem or at least point me to the right direction.

i need a regular expression pattern that does the following:

  • equal or greater than the number 3

  • must not allow decimals

  • must not allow negative numbers

i am using foundation 5 abide.js to validate my form and this requires a regex pattern for advanced validation. this is required to validate a price input field.

i been searching the net for a while without any success. i appreciate if anyone can solve this problem or at least point me to the right direction.

i need a regular expression pattern that does the following:

  • equal or greater than the number 3

  • must not allow decimals

  • must not allow negative numbers

i am using foundation 5 abide.js to validate my form and this requires a regex pattern for advanced validation. this is required to validate a price input field.

Share Improve this question asked Sep 14, 2014 at 21:40 Ahm3dAhm3d 1294 silver badges15 bronze badges 8
  • 1 why regex? you have JS. – Karoly Horvath Commented Sep 14, 2014 at 21:42
  • 3 Regex only? Much easier to convert to a number and check your constraints – Ruan Mendes Commented Sep 14, 2014 at 21:43
  • Regexes cannot calculate, so you have to express "greater than 3" in terms of characters and strings. – georg Commented Sep 14, 2014 at 21:43
  • Also, you specified the minimum, how about the max? Is 12348712983470 a valid price? – georg Commented Sep 14, 2014 at 21:48
  • 1 I love the way OPs selectively disregard questions. – Karoly Horvath Commented Sep 14, 2014 at 21:56
 |  Show 3 more ments

2 Answers 2

Reset to default 5

As you can read in ments, regex is not the appropriate tool to deal with numbers. However you can use this pattern:

^(?:[3-9]|[12]\d)\d*$

Note: if you want to allow leading zeros, you only need to add 0* at the begining:

^0*(?:[3-9]|[12]\d)\d*$ 

you could use this pattern ^(?![012]$)\d+$
Demo

here is another one for the leading zeros ^(?!0*[012]$)\d+$
Demo

本文标签: javascriptregex equal or greater than the number 3Stack Overflow