0

I have a condition to validate a number. My requirement is accept only following numbers.

0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5

I tried with this regex but it returns false for decimal numbers.

/^[0|0.5|1|1.5|2|2.5|3|3.5|4|4.5|5]$/

The system that I am working is an existing system and I must use regular expression. No other option available for me.

Can anyone help me?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Don't validate numbers with regexes... – Cerbrus Sep 16 '15 at 10:12
  • @Cerbrus What's wrong with this case? – Avinash Raj Sep 16 '15 at 10:13
  • What's wrong is that he's using regexes to validate the value of numbers. Unless he's storing the regexes in something like a database, the validation code really should be fixed to accept functions. – Cerbrus Sep 16 '15 at 10:20
  • @OP: _why_ can't you use anything but regexes? – Cerbrus Sep 16 '15 at 10:23
  • @Cerbrus, This system is already built, and I am supposed to do any code change, it will be a long process which I do not want to take. – Manoj Lasantha Sep 16 '15 at 10:26
  • Changing a validation method to accept a function instead of a regex can't possibly take more than an hour of work. – Cerbrus Sep 16 '15 at 10:27
  • Since this question is only about validating numbers with regexes, the dupe has the best answer. – Cerbrus Sep 16 '15 at 10:30
  • 1
    @Cerbrus But the dupe won't answer this question. – Avinash Raj Sep 16 '15 at 10:35
  • 1
    If you try to mark questions as dupe then all the upcomming regex questions are dupe of some which are already exists in So. – Avinash Raj Sep 16 '15 at 10:36
  • @Cerbrus, exactly!. But I am working in a very tough environment that I need to follow a very long process to push that few lines of code to PRODUCTION. So, I prefer regex to solve this issue and I found regex /^([0-4](\.5)?|5)$/ (Which answered by [still_learning](http://stackoverflow.com/users/2948765/still-learning)) works for me. Anyway, thanks for spending your valuable time for this. – Manoj Lasantha Sep 16 '15 at 10:37

3 Answers3

2

Your expression is almost correct:

/^(0|0\.5|1|1\.5|2|2\.5|3|3\.5|4|4\.5|5)$/

You need to use round brackets instead of square brackets and escape the dots. Round brackets indicate groups while square brackets define a group of single characters which may be matched. A shorter variant is this:

/^([0-4](\.5)?|5)$/

This will match any digit from 0 to 4, optionally followed by .5, or the single digit 5.

Tobias
  • 7,723
  • 1
  • 27
  • 44
0

You may try the below regex.

^(?:[0-4](?:\.5)?|5)$
  • [0-4] will match range of digits from 0 to 4.
  • (?:\.5)? optional .5

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
-1

Instead of using regexes, you should really see if you can use a function.

This would be your best option, then:

function check(num) {
    var floored = Math.floor(num);
    return (num === floored || num - 0.5 === floored) && num >= 0 && num <=5;
}

alert(1 + ' ' + check(1) + '\n' +
      5 + ' ' + check(5) + '\n' +
      6 + ' ' + check(6) + '\n' +
      1.5 + ' ' + check(1.5) + '\n' +
      4.5 + ' ' + check(4.5) + '\n' +
      5.5 + ' ' + check(5.5) + '\n' +
      5.51 + ' ' + check(5.51) + '\n' +
      -1.5 + ' ' + check(-1.5) + '\n' +
      -0.5 + ' ' + check(-0.5))
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 3
    "The system that I am working is an existing system and I must use regular expression." (source: OP) – Tobias Sep 16 '15 at 10:17
  • That doesn't mean regexes are the best option, however. Since the OP is obviously capable of getting the regexes into the JS, I doubt it's impossible to get a function in there. – Cerbrus Sep 16 '15 at 10:21
  • 1
    Seriously? -2? This is _the_ way to perform the validation the OP needs. The artificial regex restriction seems to be based merely on the assumption that modifying the validation to accept functions takes too much time. It's a personal preference instead of a hard "no can do". – Cerbrus Sep 16 '15 at 10:29