简体   繁体   English

如何限制正则表达式中的数字

[英]how to restrict numbers in regex

i have a regex ^[a-zA-Z0-9]{0,14}$ which will allow only alphanumerics.我有一个正则表达式 ^[a-zA-Z0-9]{0,14}$它将只允许字母数字。 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但是有一个问题,我不希望用户名只是数字,而这个正则表达式只接受数字,也像 56426542,所以如何只限制数字

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}.此外,您可能希望将 {0,14} 更改为 {1,14}。 Well, replace the first number with your minimum length requirement.好吧,用您的最小长度要求替换第一个数字。

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请记住,{0,14} 接受空字符串作为有效输入

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}编辑前面的表达式接受 1 到 15 个字符的输入,更好的解决方案是: ^[A-Za-z][A-Za-z0-9]{0,13}

But Grommer solution is better但 Grommer 解决方案更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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