1

I am trying to validate a field which can take input as following ways:

  1. should take 1 to 4 alpha charcters.(but should start with alpha)
  2. from 5th position to so on should take numbers.(no where from 5th should accept alphabets)
  3. in between 1-4 characters of alpha it should not allow numbers. 4.even if first 4 characters are entered it should accept.(that 4 characters should be alpha.ex:"asdf") ^[a-zA-Z][0-9]$

i have many things and searched many sites.i could not find it.please help me. Thank you in advance.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32

4 Answers4

1

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:

  1. ^ = Start of string
  2. [a-zA-Z] = a through z (case-insensitive)
  3. {1,4} = 1 to 4 times
  4. [0-9]+ = one or more numbers
  5. $ = End of string

Because each situation is different, I would suggest using an online regex tester to test certain strings of characters.

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • dear meeseeks, thanks for your answer,your regex will accept something like "asdf455555555555555".but if i give only "asdf" it wont accept.so can't take your answer – Rushi Ayyappa Oct 29 '15 at 18:26
  • @RushiAyyappa Your unedited post did not say that it should accept "asdf" so that's why I included `[0-9]+`. If you wanted `zero` or `more` numbers then it would be `[0-9]*` but your welcome anyways! :) – Mr. Meeseeks Oct 29 '15 at 18:56
0
^[a-zA-Z]{1,4}\d*
  • [a-zA-Z] is for alpha chars
  • `\d' is short for numbers
  • {1,4} specify 1 to 4 chars
  • * specify any number of digits (including none)
Cyrbil
  • 6,341
  • 1
  • 24
  • 40
0

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.

lintmouse
  • 5,079
  • 8
  • 38
  • 54
  • ,i am developing something that will take inputs of 1-4 characters .it means i can accept if he enters only one char or 4 chars. – Rushi Ayyappa Oct 29 '15 at 18:33
  • i am sorry man i cannot accept your answer because it is accepting strings likr "s6565" as i said you ,first 4 characters must and should be in characters and no of characters can be any from minimum 1 to max of 4 at starting – Rushi Ayyappa Oct 29 '15 at 19:18
  • yes,now this works awsome.but can you explain me this? thanks so much man!Happy coding! – Rushi Ayyappa Oct 29 '15 at 19:32
  • No problem, it is using a `|` to try an alternate pattern if the first one fails. So the first pattern is effectively `^[A-Za-z]{1,4}$` so it has to be only 1 - 4 characters. If that fails, it attempts the second pattern which is effectively `^[A-Za-z]{4}\d+$`, which is four characters followed by one or more digits. – lintmouse Oct 29 '15 at 19:33
  • but why did you use ?: in the beginning – Rushi Ayyappa Oct 29 '15 at 19:36
  • It means it's a non-capturing group. If you're just checking for validity, it isn't really necessary in this case. http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group – lintmouse Oct 29 '15 at 19:37
  • oops man..it is a bit tough to get into my brain..can you shortly tell me about non cpturing group? – Rushi Ayyappa Oct 29 '15 at 19:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93725/discussion-between-dustmouse-and-rushi-ayyappa). – lintmouse Oct 29 '15 at 19:43
0

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

w.b
  • 11,026
  • 5
  • 30
  • 49