Thursday, February 17, 2011

Finally got a regular expression to validate a number between zero and eleven, allowing a leading zero. It allows only two digits to be entered maximum, and you can put one whitespace character before or after a single digit. Tested in Firefox 3.6 and IE 6.

It should be simple, but "simple" wasn't working in Firefox 3.6. I didn't bother testing other browsers with the one that didn't work in Firefox.

<asp:RegularExpressionValidator ControlToValidate="txtNumberOfMonths"
ErrorMessage="Months At: Please use a rounded number from 0 to 11"
ValidationExpression="^[\s]*((0?[0-9])|(1[0-1]))[\s]*$" runat="server"/>

The simple regex that didn't work was: "\s*(0?\d|1[01])\s*