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:

  1. a name may contain of these characters: [a-za-z .'-]
  2. the name may start alphabet or apostrophe
  3. any of characters in [ .'-] may not occur more once in stretch, ie., no '---' or '--'
  4. 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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -