Hi,
I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input:
5' 9"
6'
5'8"
Any ideas?
Hi,
I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input:
5' 9"
6'
5'8"
Any ideas?
Something like:
\d'(?:\s*\d+'')?
The second part refers to optional part of the heigth.
Remove the + if you want only one digit.
\b\d'(?:\s*\d+'')?\b
can also be used to detect that pattern within a text (avoid detecting 1234'45 as an heigth for... a person?!)
You can test that regexp here for javascript.
^\d'\s?(\d{1,2}")?$
Tested here: http://www.regular-expressions.info/javascriptexample.html
If you want to make sure that no one mucks around with it, you could limit it to sensible ranges, eg: 3' to 7'11''
/^(3-7)'(?:\s*(?:1[01]|0-9)(''|"))?$/
I always thought that the "inches" mark was a double quote ("
), compared to VonC's answer where he put it as two single quotes (''
), so this regex takes both into consideration.
Ok. Thanks for all your input. Wow, that was fast, Big time.
Anyway, I've tested all your regex and it seems Ruben's answer passed all my test input. Thanks a lot for that mate.
So here's the one that I need:
^(\d{1,5})\'((\s?)(-?)(\s?)([0-9]|(1[0-1]))\")?$