0

How can i change this regular expression /^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$/ to see that it accepts "0.1" and "0.2" and so on values but it should not accept "0.0"

Someone
  • 10,405
  • 23
  • 67
  • 100
  • Are you trying to validate a decimal value to see if it's not 0.0? – BoltClock Dec 06 '10 at 21:18
  • 1
    What values exactly should or should not be accepted? – Karl Knechtel Dec 06 '10 at 21:19
  • @Bolt:Iam looking to accept this values 0.1,0.2 and so on and 0.0 is not valid for my field that iam able to validate – Someone Dec 06 '10 at 21:20
  • @Derby: "and so on" means any number greater than 0.0, or any number increasing in steps of 0.1 that is greater than 0? – BoltClock Dec 06 '10 at 21:21
  • 2
    I feel like @Derby should read [To use or not to use regular expressions?](http://stackoverflow.com/questions/4098086/to-use-or-not-to-use-regular-expressions/4098123#4098123). –  Dec 06 '10 at 21:24

3 Answers3

2

one to four digits, with or without decimal precision of 1:

/^[1-9][0-9]{0,3}(\.[0-9])?$/

0.1 through 0.9, step 0.1:

/^0\.[1-9]$/


/^[1-9][0-9]{0,3}(\.[0-9])?$|^0\.[1-9]$/
dreynold
  • 865
  • 8
  • 17
2

This really doesn't sound like a regex problem: you'd be much better off (from a maintainability and readability standpoint) treating your numbers as numbers and doing a simple range check:

var isValid = function (x) {
    // Make sure we're dealing with a number
    x = parseFloat(x);
    // If x is greater than zero but less than 0.9
    // this expression evaluates to true so the
    // function returns true, otherwise it evaluates
    // to false so the function returns false.
    return (x > 0 && x <= 0.9);
};

Sure, it doesn't use any regexes (which is what you were asking for) but there's no good reason to use them to solve this problem (mostly because it is much more easily solved using the above method)

xj9
  • 3,381
  • 2
  • 24
  • 23
1

RegExp covering existing cases, but also allowing decimals 0.1 <= n <= 0.9.

/^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}\.[0-9]$|^0\.[1-9]$/

or better, from Dreynold's solution:

/^[1-9][0-9]{0,3}(\.[0-9])?$|^0\.[1-9]$/

Old Answer, for reference, requirements updated by OP:

If you only want a single decimal place, with the existing allowed values:

/^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$|^0\.[1-9]$/

If just the decimal then:

/^0\.[1-9]$/

If multiple decimals then:

/^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$|^0\.[1-9][0-9]*$/

or

/^0\.[1-9][0-9]*$/
Orbling
  • 20,413
  • 3
  • 53
  • 64
  • @orbling:It is accepting more than one Decimal after the point – Someone Dec 08 '10 at 17:45
  • @Derby: Modified with multiple decimal versions. Note that it'll block 0.05, if you need values below 0.1, then it needs another clause. – Orbling Dec 08 '10 at 17:51
  • @orbling: I need some thing like this only one decimal is valid after the point that is 1.9 and 2.9 but not 2.99(invalid) and 0.1 is valid – Someone Dec 08 '10 at 17:55
  • @orbling:It should accept only 1 digit after decimal .and the The 0.1 to 0.9 should be valid – Someone Dec 08 '10 at 17:58
  • That is the exact opposite of what you said not 13 minutes before. Answer revised. Is `2.0` or `134.0` valid? – Orbling Dec 08 '10 at 18:10
  • @orbling:which answer should i consider /^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}\.[1-9]$|^0\.[1-9]$/ – Someone Dec 08 '10 at 18:22
  • @Derby: `/^[1-9][0-9]{0,3}(\.[0-9])?$|^0\.[1-9]$/` - which was dreynold's solution from yesterday. – Orbling Dec 08 '10 at 18:34