Reputation: 2511
I am trying to use regex to validate user input with my JqueryUI validator. I would like the user to ONLY be able to enter an integer or a correctly entered fraction.
I created regexr using \d+.\d+|\d+.$|^.0$|\d+/[1-9]+|\d+ \d+/[1-9]+ but it is not passing my tests because it accepts decimals and odd combos. I got it from another SO post.
I would basically like the only acceptable input to be:
an integer "1" or "200"
fraction "1/2" or "3/8"
mixed number "1 1/2" or "200 3/8"
I would NOT like it to accept:
any letters or other characters "number" or "5%"
odd combinations of correct entries "1 1/2 4" or "1 1/2 1/2"
How can I create proper regex that passes the tests on http://regexr.com/39hlm? (at the very least I would like it to accept integers and mixed numbers only and I can handle the other cases)
Upvotes: 0
Views: 633
Reputation: 7948
without verifying "proper fractions" use this pattern
^(\d+(?:(?: \d+)*\/\d+)?)$
Upvotes: 1