Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restrict numbers in regex

Tags:

c#

regex

i have a regex ^[a-zA-Z0-9]{0,14}$ which will allow only alphanumerics. but there is problem i dont want a username to be only numbers and this regex is accepting only numbers also like 56426542 so how to restrict only numbers

like image 590
aquib.qureshi Avatar asked Dec 05 '25 13:12

aquib.qureshi


2 Answers

The regex you want:

^((?=.*[a-zA-Z])[a-zA-Z0-9]{0,14})$

Regex sandbox to play in with it.

This won't force you to have to start with a letter. The username can start with a letter or a number, an ensures that the username isn't only numbers.

Edit: fixed the length check. Also, you might want to change the {0,14} to {1,14}. Well, replace the first number with your minimum length requirement.

like image 103
Gromer Avatar answered Dec 08 '25 05:12

Gromer


TIMTOWTDI

The simpliest solution is to require a username to start with a letter

^[A-Za-z][A-Za-z0-9]{0,14}

Keep in mind that {0,14} accepts empty string as a valid input

Edit The previous expression accepts 1 to 15 characters of input a little better solution is:^[A-Za-z][A-Za-z0-9]{0,13}

But Grommer solution is better

like image 41
rbernabe Avatar answered Dec 08 '25 07:12

rbernabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!