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)
精彩评论