tags:

views:

79

answers:

2

The input XML tag must be validated for a pattern which is like this:

type : positive int / decimal, minimum length is 0, max length is 12(before decimal point), fraction digits are optional if exist then precision must be 2. This means both positive integer and Decimal numbers(2 digit precision) are allowed.

so the acceptable values can be like,
null, 0, 0.00, 1234567890, 123456789012, 123456789012.12,

invalid values are:
0.000, 1234567890123(13 digits - invalid),

The pattern I have designed is:

<xs:pattern value="|([0-9]){12}|([0-9]){12}[.][0-9][0-9]"/>

The problem with this pattern is, it doesn't allow the number with string-length less than 12, it says "1234567890" is an invalid value, where as it must be allowed!

+2  A: 

The regex pattern \d{0,12}(\.\d{2})? should do the trick.

A short explanation of the pattern:

\d is shorthand for [0-9]. \d{0,12} indicates that \d should occur 0 to 12 times. It is greedy, so it will try to match as many as it can, up to 12.

\.\d{2} represents a dot followed by exactly two digits.

? indicates that the previous item can occur 0 or 1 time. The brackets around (\.\d{2}) are necessary because without them ? would apply only to \d{2}

Toon Van Acker
+1  A: 

But the above would allow:

.00 as valid input. Perhaps something like: (|\d{1,12}(\.\d{2})?) to allow null or at least a single digit would be better.

Gavin