Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple regex that matches x or x+y

Tags:

jquery

regex

I'm trying to validate an input field to accept x OR x+y where x and y are whole numbers from 1-10. Which regular expression matches this? This is to be used with custom jQuery validation.

Examples

  • 4: OK
  • 5+1: OK
  • 0: Not OK
  • 0-9: Not OK
  • 1+11: Not OK
like image 287
stef Avatar asked Dec 22 '25 11:12

stef


2 Answers

This should do it (assuming your "1-10" was inclusive of both ends):

^([1-9]|10)(\+([1-9]|10))?$

http://rentzsch.github.com/JSRegexTeststand/ is your friend here.

like image 75
kevingessner Avatar answered Dec 24 '25 00:12

kevingessner


This one should do the trick

/^([1-9]|10)(\+([1-9]|10)){0,1}$/
like image 40
devnull69 Avatar answered Dec 24 '25 01:12

devnull69



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!