开发者

preg_match string must start with alphabetical char

开发者 https://www.devze.com 2023-02-10 16:49 出处:网络
I have a name field and I want to validate, so the first char must be alphabetical of开发者_JS百科 the name and allow spaces and dashes after. I have this so for but it will allow \"-\" as first char.

I have a name field and I want to validate, so the first char must be alphabetical of开发者_JS百科 the name and allow spaces and dashes after. I have this so for but it will allow "-" as first char.

preg_match("/^([a-z -])+$/i", $str)

Thanks


Being specific is the better option. But you can also use an assertion:

preg_match("/^(?![- ])([a-z -])+$/i", $str)

The (?!..) can exclude characters from occuring first. You could also use a positive assertion like (?=\w).


preg_match("/^[a-z][a-z -]+$/i", $str)
0

精彩评论

暂无评论...
验证码 换一张
取 消