0

I want to have a range between 0 to 65536 , what would be the regular expression for that?

1
  • 2
    Using a regular expression to check if number falls in a range is like using a hammer to open an orange. Commented Sep 14, 2010 at 16:06

3 Answers 3

9

Don't use a regular expression.

if(i >= 0 && i <= 65536)
Sign up to request clarification or add additional context in comments.

1 Comment

@mazhar: JQuery changes neither the fact that regexps are not suited for this nor does it change if statements.
1

A regular expression really isn't well suited to this sort of validation. Gareth's answer provides a much more sensible solution.

If, for some reason, you absolutely have to use a regex then it will probably look something like this:

^(?:[0-5]?[0-9]{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6])$

1 Comment

No wait, the other thing. Horrific ;) I suppose it needed to be seen to compare the difference though
1

See also this question, which asks pretty much the same thing, and got pretty much the same answer. (ie don't use regex for this!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.