简体   繁体   中英

Regular Expression that only integers or mixed numbers match

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.

http://regexr.com/39hlm

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)

without verifying "proper fractions" use this pattern

^(\d+(?:(?: \d+)*\/\d+)?)$  

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM