Swati Awasthi
Swati Awasthi

Reputation: 237

I want a javascript regular expression which accepts only numbers 0 or 0.0 / 0.5 / 1 or 1.0 /1.5 /2 or 2.0/ 2.5 / 3 or 3.0 /3.5 /4 or 4.0/ 4.5 / 5

I want a javascript regular expression which accepts only numbers

0 or 0.0 / 0.5 / 1 or 1.0 /1.5 /2 or 2.0/ 2.5 / 3 or 3.0 /3.5 /4 or 4.0/ 4.5 / 5

I'm using the below expression, it works fine but the only condition it fails when i give 5.0 or 5.1 or 5.2 or 5.3 or 5.4 or 5.5:

var regOrderNo = /^[0-5]+(\.[05])?$/;

I want a regular expression that should not allow decimal after number 5

Upvotes: 0

Views: 974

Answers (1)

cHao
cHao

Reputation: 86504

var regOrderNo = /^(5|[0-4](\.[05])?)$/;

Since you don't want 5 to be treated like the others, you need to have a separate case for it.

Also, note that the + is gone. Your regex could have matched 123123123.5. You want to make sure there's only one digit.

Upvotes: 2

Related Questions