JohnK
JohnK

Reputation: 21

Regex string: specific requirement

I am trying to get my regex string right but I don't seem to get it working as it should.

I have a numeric field that should have 5 digits. The digits can start only with 04xxx and 5xxxx

This string is not covering it completly:

/[05][0-9][0-9][0-9][0-9]/

it forces to start with 0 or 5 followed by 4 digits, but it allows for example 012345

Any ideas?

Upvotes: 2

Views: 36

Answers (2)

user557597
user557597

Reputation:

This looks more like a validation -

^(?=04|5)\d{5}$

Expanded:

 ^               # BOS
 (?= 04 | 5 )    # Lookahead, starts with '04' or '5'
 \d{5}           # Match 5 digits
 $               # EOS

Upvotes: 1

Bond
Bond

Reputation: 16311

Try this for your regex pattern:

^(04|5\d)\d{3}$

Upvotes: 4

Related Questions