简体   繁体   中英

Regular expression to limit character & number

my regular expression requirement is number and character combined exp. and this limited 5~15 characters.

for example,

abcd1, abc1d, a21ab, 1abcd, abcvda123 ...
ABCd1, Abc1d, 

not allowed exampled,

abcd!1, !adf2a, abcd!a, abcd!2 ...
abc1, ab1c, 1abc, !abc ...

my regex exp is

^(?=.+[a-zA-Z])(?=.+[0-9]).{5,15}$

but is too bad.

Change .{5,15} to [a-zA-Z0-9]{5,15} .

Also, in the lookaheads, change .+ to .* . Otherwise, it won't match if the only character of the type after that is the first character in the string. So the resulting regexp is:

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

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM