Reputation: 193
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.
Upvotes: 0
Views: 3101
Reputation: 626802
You can use this regex with ng-pattern
:
ng-pattern="/^\b[A-Z][a-z]*(\s*\b[A-Z][a-z]*\b)*$/"
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>
Upvotes: 1