Tales
Tales

Reputation: 303

Javascript: regex for positive and negative number but no decimal point

I want a regex which only accepts positive and negative numbers. It should not include decimal point or any other characters except positive and negative numbers.

I have tried below regex but it accepts decimal point.

^-?\d*\.{0,1}\d+$

The regex should not allow any decimal point even it is before 0 e.g. 122334.0000

Thank you.

Upvotes: 0

Views: 298

Answers (2)

Pushprajsinh Chudasama
Pushprajsinh Chudasama

Reputation: 7949

You can do something like the below code.

^\-?[1-9]\d{0,2}$

OR

^[+-]?\d+$

Upvotes: 1

Pushprajsinh Chudasama
Pushprajsinh Chudasama

Reputation: 7949

You can try below regx.

^-?\d+$

Upvotes: 1

Related Questions