number validation regex in XSD
I need to be able to validate that a number in and XSD looks like so: \d{0,15}(.\d\d)?
So, up to 15 digits followed by an optional decimal with two digits following.
The catch is that I want it to not accept numbers like these:
0.00
0
00000
000.00
What is a good way to do this, and use it in our XSD?
Thanks, 开发者_StackOverflowAlex
You could prefix the regex with a negative lookahead:
(?!0*(\.00)?)
to give:
^(?!0*(\.00)?$)\d{0,15}(.\d\d)?$
Note that this will also reject an empty string.
You're very precise about the things you want to accept, but rather imprecise about what you want to reject. Is it that you want to reject anything that has the decimal value zero, regardless how it is written? In that case the solution would be to have a <minExclusive value="0"/>
facet alongside the pattern facet. In fact, I'm not sure you can't achieve the whole thing using the totalDigits and fractionDigits facets.
How about
[1-9]\d{0,14}(\.\d{2})?
I would also second what @MRAB implied. Your regex, as is, will match the empty string (zero digits followed by zero "dot-digit-digit"s). This one requires at least one digit from 1-9 be present, and that any number with more than one digit have a first digit between 1 and 9.
精彩评论