Thread: Check if the input is either -1 or 1 to 32767

1. Check if the input is either -1 or 1 to 32767

I am trying to create an expression that checks if the input number is
a) either -1
b) either in the range of 1-32767
c) nothing else than specified above
I have following expression which tests true for anything greater than 32767 but fails for 0 or -1.
Code:
```var expr = /^([1-2]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}|3[0-1]{1}[0-9]{1}[0-9]{1}[0-9]{1}|32[0-6]{1}[0-9]{1}[0-9]{1}|327[0-5]{1}[0-9]{1}|3276[0-7]{1})\$/;
var myresult = expr.test(0);
Any pointer...???
Last edited by shleda; February 4th, 2009 at 08:22 AM. Reason: TyPo5 :P
2. No Profile Picture
User 165270
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2005
Posts
497
Rep Power
940
Originally Posted by shleda
I am trying to create an expression that checks if the input number is
a) either -1
b) either in the range of 1-32767
c) nothing else than specified above
I have following expression which tests true for anything greater than 32767 but fails for 0 or -1.
Code:
```var expr = /^([1-2]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}|3[0-1]{1}[0-9]{1}[0-9]{1}[0-9]{1}|32[0-6]{1}[0-9]{1}[0-9]{1}|327[0-5]{1}[0-9]{1}|3276[0-7]{1})\$/;
var myresult = expr.test(0);
Any pointer...???
First, I must say that validating numerical values with regex, is not the preferred way to go.

Having said that, a couple of remarks about your current approach:
- "{1}" can be removed, they only clutter up your regex
- "{0,1}" can be replaced by "?"
- this: "[0-9]{0,1}[0-9]{0,1}[0-9]{0,1}[0-9]{1}" can be replaced by: "[0-9]{0,4}"
- "[0-9]" can be replaced by "\d"
- a range like "[1-2]" (two successive numbers) can be replaced by "[12]"

If you apply all those things (and a bit more to reject "0" and accept "-1"), you get this:

Code:
`^(-1|[12]?[1-9]\d{0,3}|3[01]{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])\$`
Good luck!
Last edited by prometheuzz; February 4th, 2009 at 09:45 AM.
3. No Profile Picture
User 165270
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2005
Posts
497
Rep Power
940
This might be a bit more intuitive:

Code:
```// regex:
^(-1|[1-9]\d{0,3}|[12]\d{4}|3[01]\d{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])\$

// explanation:
^                 // start of the string
(                 // start group 1
-1              //   matches -1
| [1-9]\d{0,3}  //   or matches 1-9999
| [12]\d{4}     //   or matches 10000-29999
| 3[01]\d{3}    //   or matches 30000-31999
| 32[0-6]\d{2}  //   or matches 32000-32699
| 327[0-5]\d    //   or matches 32700-32599
| 3276[0-7]     //   or matches 32760-32767
)                 // end group 1
\$                 // end of the string```
4. Whoa!!!

Originally Posted by prometheuzz
This might be a bit more intuitive:

Code:
```// regex:
^(-1|[1-9]\d{0,3}|[12]\d{4}|3[01]\d{3}|32[0-6]\d{2}|327[0-5]\d|3276[0-7])\$

// explanation:
^                 // start of the string
(                 // start group 1
-1              //   matches -1
| [1-9]\d{0,3}  //   or matches 1-9999
| [12]\d{4}     //   or matches 10000-29999
| 3[01]\d{3}    //   or matches 30000-31999
| 32[0-6]\d{2}  //   or matches 32000-32699
| 327[0-5]\d    //   or matches 32700-32599
| 3276[0-7]     //   or matches 32760-32767
)                 // end group 1
\$                 // end of the string```
It is really intuitive and added some thing to my knowledge. How do I rate your post?
5. No Profile Picture
User 165270
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2005
Posts
497
Rep Power
940
Originally Posted by shleda
It is really intuitive and added some thing to my knowledge. How do I rate your post?
There's a scale on the upper right corner of each post where you can indicate how helpful (or unhelpful) a post is.
Good to hear my post was helpful to you!