Validate a string using regular expressions - 1-30 with up to 2 decimal places

As the title suggests I need a regular expression that can validate an input string to make sure it is a number between 1-30 and up to 2 decimal places.

For instance,

4 is fine
10.25 is fine
15.3 is fine
29.99 is fine
30 is fine
30.01 is not fine

EDIT: so it has to be a regular expression due to the limitations of the system I'm using, I have tried several things,

I can get as far as


which allows up to a 2 digit number with up to 2 decimal places.


EDIT: For your specific situation of needing a regex, try this:



^ ... $ - To make sure the regex starts and ends at the start and end of the string.

(?!0) - negative lookahead to ensure we don't start with a 0

(30(\.0{1,2})? - 30 optionally followed by .0 or .00

| Or...

[12]? Tens digit of 1 or 2 only

\d One non-optional units digit

(\.\d{1,2})? Optional .digit or .digitdigit

Unfortunately, this regex is not easily tunable to fit any range of numbers. (It would be an interesting project to write a program to automatically spit out regexes like this one.)

Otherwise I would say: Don't re-invent the wheel.

double result;
if (double.TryParse(inputstring, out result))
    if (result >= 1.0 & result <= 30.0)
        return true;
return false;

If it's not two decimal places and you need it to be, you can calculate Round(result, 2) so it is.

Need Your Help

Google Calendar crashes because I added an event with endTime earlier than startTime

android calendar google-calendar

So I added an event to my Google Calendar using the CalendarProvider according to this example.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.