0

I am trying to use a regular expression which will validate input for values between 0.00 and 15.00 to a maximum of 2 decimal places.

Example of valid and invalid data:

Valid Data:

  • 0.00
  • 13.
  • 1.01
  • 14.99
  • 15

Invalid Data:

  • 15.01
  • 13.111
  • -1.14
  • 0.001
  • 00.02
  • .25
  • 6
    Not sure where you're using this, but in general you'd be better off parsing and doing numerical comparisons. – juharr Jun 28 '17 at 19:00
  • 2
    **You do not want regular expressions for this** – maccettura Jun 28 '17 at 19:02
  • Why is `0.001` invalid but `15` are `13.` are valid? – gunr2171 Jun 28 '17 at 19:03
  • Because i need it to validate up to 2 decimal places not 3 – user1990265 Jun 28 '17 at 19:04
  • @user1990265 But 0.001 is between 0.00 and 15.00. If you round it it would just be 0.00, or do you just mean that anything with more than 3 decimal places is invalid, like 4.555. – juharr Jun 28 '17 at 19:05
  • But i don't want a user adding 0.001. The regex needs to only allow up to 2 decimal places. – user1990265 Jun 28 '17 at 19:06
  • @user1990265 So the user wouldn't be allowed to enter 4.555 either? – juharr Jun 28 '17 at 19:07
  • No not 4.555 but 4.55 would be ok – user1990265 Jun 28 '17 at 19:07
  • Also are leading zeros allowed like 0005.2? Or what about nothing on the left of the decimal like .25? – juharr Jun 28 '17 at 19:07
  • Leading zeros are not allowed no. Must be 0.00 or greater. – user1990265 Jun 28 '17 at 19:08
  • 1
    Maybe something like `^([0-9]|1[0-4])(\.[0-9]{0,2})?|15(\.0{0,2})?$`? – juharr Jun 28 '17 at 19:11
  • And nothing on the left of the decimal like .25 not allowed. But 10 followed by a decimal point for example (10.) should be allowed. – user1990265 Jun 28 '17 at 19:11
  • 2
    Also if this is for limiting the input of a text box you might want to look into other UI controls that are specifically made for entering a range of numbers. – juharr Jun 28 '17 at 19:13
  • 1
    @AlexeiLevenkov - Just a heads up. Neither of those linked-to duplicates is an answer to the OP's question. And, the titles may look good, but I'd wager you _can't_ generalize any regex numeric range. If you can please post that generalization. –  Jun 28 '17 at 22:04
  • 1
    @sln "don't use regex" is good solution (from -90/90 one) - already cloned to this q... But if you really into that stuff you'd use search engine to https://www.bing.com/search?q=regex+numeric+range and than read http://www.regular-expressions.info/numericranges.html or just use tool - https://stackoverflow.com/questions/4101053/generate-a-regexp-from-a-numeric-range. If you simply advocating to downvote post due to no-research-shown - go ahead. – Alexei Levenkov Jun 28 '17 at 22:28
  • @sln the question really had only two options - insane number of downvotes for asking for code showing zero research (and possibly closed as "too broad" at the end) or quickly closed as duplicate and effectively disappear with 0 votes. I don't see why negative votes are much better than what I think are duplicates. Based on current vote count it does not look you consider question well researched nor find answer not actually answering the question. So essentially you fine with "don't use regex" answer, but for some reason want it to be unique answer instead of dup - vote to reopen than. – Alexei Levenkov Jun 28 '17 at 23:58
  • @sln I've posted q on meta https://meta.stackoverflow.com/questions/351422/duplicate-disagreement-give-me-code-for-range-regex-q - feel free to edit the question and you are really welcome to provide answer there. Thanks for feedback. – Alexei Levenkov Jun 29 '17 at 00:17

1 Answers1

1

Don't use regex. Parse the value as a double and do math comparison.

if(double.TryParse(yourString, out double value))
{
    if(value >= 0 && value <= 15)
    {
        //it's a good value
    }
}
// else, it's either not a double or out of range
gunr2171
  • 16,104
  • 25
  • 61
  • 88