admin管理员组文章数量:1415673
What are the JavaScript and PHP regex patterns if a string is to fulfill the following conditions:
- The string should be between (and including) 4 and 20 characters
- It can contain only lowercase alphabets and, optionally, digits.
- It MUST contain at least 1 alphabet
The following strings formats are acceptable :
randy
randy39
39randy
r789456123
The following are NOT acceptable:
ran
3546
r_andy
__3912
What are the JavaScript and PHP regex patterns if a string is to fulfill the following conditions:
- The string should be between (and including) 4 and 20 characters
- It can contain only lowercase alphabets and, optionally, digits.
- It MUST contain at least 1 alphabet
The following strings formats are acceptable :
randy
randy39
39randy
r789456123
The following are NOT acceptable:
ran
3546
r_andy
__3912
- 5 Have you tried something to solve your task or you just came to SO for munity to do your work? – zerkms Commented Jan 3, 2012 at 11:23
- I've tried /^([a-z0-9]){3,5}$/ – dinchakpianist Commented Jan 3, 2012 at 11:25
- Try /^([a-z0-9]{4,20})$/ if you need the capturing brackets, or /^[a-z0-9]{4,20}$/ if you don't, but that doesn't test for at least 1 alpha – Mark Baker Commented Jan 3, 2012 at 11:30
2 Answers
Reset to default 6You could use a lookahead assertion to verify that the string contains a letter somewhere.
/^(?=.*[a-z])[a-z0-9]{4,20}$/
This should work in both JavaScript and PHP alike.
Use two regexes: /^[a-z0-9]{4,20}$/
and /[a-z]/
(the actual syntax might differ between PHP and Javascript)
本文标签:
版权声明:本文标题:php - Regex pattern for lowercase letters, optional digits, and constrained length - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745217761a2648250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论