vjangra
vjangra

Reputation: 193

Regex to validate that first letter of every word is capital in angular js

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

Answers (3)

Uri Y
Uri Y

Reputation: 850

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

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

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>

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13640

You can use

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

See Demo and Explanation

Upvotes: 0

Related Questions