Reputation: 2848
I am trying to validate a field which can take input as following ways:
i have many things and searched many sites.i could not find it.please help me. Thank you in advance.
Upvotes: 1
Views: 3168
Reputation: 5119
Try this:
^[A-Za-z]{1,4}\d*$
https://regex101.com/r/yH6pR3/1
It will only allow 1-4 alpha characters, and then only digits thereafter. Digits are optional. You can change that by making it \d+
instead.
I am wondering what you mean by digits only from 5th position onward. What if there are three or less alpha at the start of the string?
UPDATE:
^(?:[A-Za-z]{1,4}|[A-Za-z]{4}\d+)$
https://regex101.com/r/qQ8nR2/1
First it attempts to match just 1-4 characters. If that fails, then it attempts to match 4 characters followed by 1 or more digits.
Upvotes: 0
Reputation: 6478
^[a-zA-Z]{1,4}\d*
[a-zA-Z]
is for alpha chars{1,4}
specify 1 to 4 chars*
specify any number of digits (including none)Upvotes: 0
Reputation: 11228
You can also write it this way:
^\p{L}{1,4}\d+$
\p{L}{1,4}
matches any letter 1 to 4 times
\d+
matches any digit one or more times
Upvotes: 0
Reputation: 1851
For an answer off the top of my head:
^[a-zA-Z]{1,4}[0-9]+$
will match a string with the following break down:
^
= Start of string[a-zA-Z]
= a
through z
(case-insensitive){1,4}
= 1 to 4 times[0-9]+
= one
or more
numbers$
= End of stringBecause each situation is different, I would suggest using an online regex tester to test certain strings of characters.
Upvotes: 1