Omer
Omer

Reputation: 15

RegEx: How to limit a string to be either x characters OR y characters

Been trying to set a field's behavior limitation to allow users to only enter 11 or 14 digits. the best I came up so far is to limit the range to be between 11 and 14, but I need it to be either or.

^([0-9]{11,14})+$

Can anyone help?

Upvotes: 0

Views: 82

Answers (3)

Toto
Toto

Reputation: 91508

Another way to do the job:

^[0-9]{11}(?:[0-9]{3})?$

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115242

You can use | for alternation

^([0-9]{11}|[0-9]{14})$

The above regex match either 11 or 14 digit string

Upvotes: 2

ndnenkov
ndnenkov

Reputation: 36110

Use or (|):

^([0-9]{11}|[0-9]{14})$

Upvotes: 3

Related Questions