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


as title suggests need regular expression can validate input string make sure number between 1-30 , 2 decimal places.

for instance,

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

edit: has regular expression due limitations of system i'm using, have tried several things,

i can far

^\d{1,2}(\.\d{1,2})?$  

which allows 2 digit number 2 decimal places.

edit: specific situation of needing regex, try this:

^(?!0)(30(\.0{1,2})?|[12]?\d(\.\d{1,2})?)$

breakdown:

^ ... $ - make sure regex starts , ends @ start , end of string.

(?!0) - negative lookahead ensure don't start 0

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

| or...

[12]? tens digit of 1 or 2 only

\d 1 non-optional units digit

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

unfortunately, regex not tunable fit range of numbers. (it interesting project write program automatically spit out regexes one.)


otherwise say: don't re-invent wheel.

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

if it's not 2 decimal places , need be, can calculate round(result, 2) is.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -