Pradeep
Pradeep

Reputation: 2729

Regular expression to validate number greater than 0 and less than 1999

I need a Regular expression to validate number greater than 0 and less than 1999.

I tried the below code but it require LiveValidation and lot of code.

var f8 = new LiveValidation('f8');
f8.add( Validate.Numericality, { minimum: 0, maximum: 1999} );

Thanks

Upvotes: 1

Views: 6276

Answers (5)

NeverHopeless
NeverHopeless

Reputation: 11233

Check out this pattern:

^([0-9]{0,3}|1\d[0-8][9]|1\d{2}[0-8])$

It will allow values between 1 and 1998, inclusive.

Upvotes: 1

elclanrs
elclanrs

Reputation: 94101

I wouldn't do this with regex but try:

/^(?![2-9].{3})\d{1,4}$/

Again, this in unnecesary, but you get the idea.

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57660

Just think how much time you are wasting just by looking for an answer to compare a number with regular expression. But I think as a programmer you know that >< symbols are in every language to compare numbers. I recommend you use those.

function is_valid(strNum){
    var num = parseInt(strNum);
    return (num>0 && num<1999);
}

This code will do what you need and it'll not even waste time

Upvotes: 1

ovabrandon
ovabrandon

Reputation: 986

How about

([1-9][0-9]{0,2}|1[0-8][0-9]{2}|19[0-8][0-9]|199[0-8])

Upvotes: 2

Nikola Petkanski
Nikola Petkanski

Reputation: 4794

Have you tried something like this:

^[0-1]?[0-9]{0,3}$

Upvotes: 6

Related Questions