Search code examples
phpregexvalidationpreg-matchcpu-word

Validate that a string contains only space-delimited alphanumeric words which do not start with a digit


I want to use preg_match() such that there should not be special characters such as @#$%^&/ ' in a given string.

For example :

  • Coding : Outputs valid
  • : Outputs Invalid (string beginning with space)
  • Project management : Outputs valid (space between two words are valid)
  • Design23 : Outputs valid
  • 23Designing : Outputs invalid
  • 123 :Outputs invalid

I tried but could not reach to a valid answer.


Solution

  • Try

    '/^[a-zA-Z][\w ]+$/'