regex - Proper validation of the Name entered by a user -
i trying validate name entered user. need make sure name entered 'literally' valid. tried many regular expressions within limited knowledge, none of them seem work fully. example /^[^.'-]+([a-za-z]+|[ .'-]{1})$/
since php website i'm working on, in english, english names allowed. rules applicable filtering name are:
- a name may contain of these characters:
[a-za-z .'-]
- the name may start alphabet or apostrophe
- any of characters in
[ .'-]
may not occur more once in stretch, ie., no '---' or '--' - a space should not follow
-
or'
nor come before.
or-
can please provide proper regular expression implement these?
here's regex solve problem (demo @ regex101):
/^(?!.*(''| |--|- |' | \.| -|\.\.|\n))['a-z][- '.a-z]*$/gi
breakdown:
(?!.*(''| |--|- |' | \.| -|\.\.|\n))
negative lookahead ensure no doubled characters found
['a-z]
start 1 of these characters
[- '.a-z]*
rest can include spaces , dashes, , not required (*
instead of +
)
Comments
Post a Comment