mbrc
mbrc

Reputation: 3963

REGEX percentage 2 decimal with + and minus value

This is my regex to check percentage with 2 decimals up to 100

^-?(100([.]0{1,2})?)$|(^\d{1,2}([.]\d{1,2})?)

but is working only in 99.16 and not like -99.16

How to allow also minus percentage?

Upvotes: 4

Views: 950

Answers (2)

SCouto
SCouto

Reputation: 7928

You've just missed the ^-? in the second option

^-?(100([.]0{1,2})?)$|(^-?\d{1,2}([.]\d{1,2})?)

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

You have two alternatives:

  • special case of 100 precent
  • standard case of 0..99 precent

both alternatives can have minus sign (-):

  (^-?(100([.]0{1,2})?)$)|(^-?[0-9]{1,2}([.][0-9]{1,2})?$)

as a remark, you probably want [0-9] when matching digits not any character treated as digit '\d' which include, say, Persian digits ۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷

Upvotes: 3

Related Questions