I have the following regular expression to validate blood pressure values in the form of systolic/diastolic:
\b[0-9]{1,3}\/[0-9]{1,3}\b
The expression works with the only flaw that it allows more than one non-consecutive slash (/). For example, it allows this 2/2/2
. I want it to allow only the format of a number from 1 to 999, and slash, and again a number from 1 to 999. For example, 83/23, 1/123, 999/999, 110/80, etc. Can anybody give me some help with this?
The only other expression I've found is here: ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$
, but it doesn't work.
BTW, I'm using jquery.
Thanks.
Use ^
and $
to match the beginning and end of the string:
^\d{1,3}\/\d{1,3}$
By doing so, you force the matched strings to be exactly of that form.
Don't use the \b
word-boundaries because a slash counts as a word boundary.
The use of ^
and/or $
is likely your most simple solution. Unfortunately, if your input is a part of a string or sentence or occurs more than once in a line, etc., you've got more thinking to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With