user609511
user609511

Reputation: 4261

Regex decimal that accepted point and comma

I have a regex Decimal that accepted only(.) i want to accepted also (,). that meant the user can enter value 0.1 or 0,1

"^[0-9]{1,5}(\.[0-9]{0,2})?$"

how can I modified this regex to accepted , also

Upvotes: 0

Views: 87

Answers (4)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try this

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

Regex Demo

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Just add the comma and dot inside the character class to handle both []

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

But I am not sure why you are checking {0,2} at the end? Which means only it will accept 1111. as a valid input. So better make it {1,2}

Upvotes: 0

Joey
Joey

Reputation: 354546

You can use a character class:

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

Upvotes: 0

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12797

Replace \. by [.,]. You'll get the following regex

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

Upvotes: 3

Related Questions