[英]Regex - At least one alphanumeric character and allow spaces
I want to make sure a first name field has at least one alphanumeric character and also allow spaces and dashes. 我想确保名字字段至少包含一个字母数字字符,并且还允许使用空格和破折号。
**VALID**
David
Billie Joe
Han-So
**INVALID**
-
Empty is also invalid
To ensure the dashes and spaces happen in legitimate places, use this: 为确保破折号和空格出现在合法的地方,请使用以下命令:
(?i)^[a-z]+(?:[ -]?[a-z]+)*$
(?i)
puts us in case-insensitive mode (?i)
使我们进入不区分大小写的模式 ^
ensures we're at the beginning of the string ^
确保我们在字符串的开头 [az]+
matches one or more letters [az]+
匹配一个或多个字母 [ -]?[az]+
matches an optional single space or dash followed by letters... [ -]?[az]+
匹配可选的单个空格或破折号,后跟字母... (?:[ -]?[az]+)*
and this is allowed zero or more times (?:[ -]?[az]+)*
并且允许零次或多次 $
asserts that we have reached the end of the string $
断言我们已经到达字符串的末尾 You mentioned alphanumeric
, so in case you also want to allow digits: 您提到了alphanumeric
,因此万一您还希望允许数字:
(?i)^[a-z0-9]+(?:[ -]?[a-z0-9]+)*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.