0

I am using angularjs. I want to use validation for my name field. I am a beginner in regex expressions. I want that the first letter of every word should be capital. For E.g Naveen Kumar should be valid and Naveen kumar is invalid.

I am using ng-pattern to validate the name field. What regex expression should i use? Appreciate your help.

vjangra
  • 173
  • 1
  • 5
  • 14

3 Answers3

1

You can use this regex with ng-pattern:

ng-pattern="/^\b[A-Z][a-z]*(\s*\b[A-Z][a-z]*\b)*$/"

Demo

This regex will match only entries that have words (that do not contain digits or underscore) in title case only. Thus, Avinash Raj1 or Avinash_Raj Raj will fail the validation.

Example code:

<label>Single word:
    <input type="text" name="input" ng-model="example.text"
           ng-pattern="/^\b[A-Z][a-z]*(\s*\b[A-Z][a-z]*\b)*$/" required ng-trim="false">
</label>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You can use

^\b(?:[A-Z]\w+\b(?:\s*)?)+$

See Demo and Explanation

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
0

How about ^(\b[A-Z]\w*\s*)+$? See https://regex101.com/r/qP5xG5/1

Uri Y
  • 840
  • 5
  • 13