<p>You could easily add client side (JS) validation before the user registration form is submitted. How to do that depends on your JS architecture, I'd use something like vee-validate with a regex validation.</p>

<p>However it would probably be more user friendly to just automatically do the title casing in a custom module, adding something like the following inside the module's init() function:</p>

<pre><code>Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_BEFORE_SAVE_ELEMENT, function(Event $event) {
    if ($event-&gt;element instanceof \craft\elements\User) {
        $event-&gt;element-&gt;firstName = \craft\helpers\StringHelper::titleize($event-&gt;element-&gt;firstName);
        $event-&gt;element-&gt;lastName = \craft\helpers\StringHelper::titleize($event-&gt;element-&gt;lastName);
    }
});
</code></pre>

<p>Just beware if you have international users. Who knows what this will do to their names. See <a href="https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/" rel="nofollow noreferrer">Falsehoods programmers believe about names</a></p>

CC BY-SA 4.0
2019-04-12T12:49:02.623Z
Thanks Udo, that sounds reasonable. That blog post is also a real eye-opener, thanks for sharing!
Jamie Wade
CC BY-SA 4.0
2019-04-12T09:04:04.477Z
2019-04-12T09:46:38.360Z
2019-04-12T09:46:38.360Z
Udo
<p>We have developed a Craft 3 site that has user registration. Once a user account has been confirmed and activated, they are shown in a Contacts directory, which other users can search through.</p>

<p>As the user is responsible for adding their own details, a few users have submitted their names in lowercase:</p>

<p><code>jamie wade</code> instead of <code>Jamie Wade</code>.</p>

<p>Whilst there is nothing <em>wrong</em> with this, it doesn't look great. Is there anyway we can enforce capitalisation in Craft on the users First Name and Last Name fields?</p>

<p>I am aware of <a href="https://twig.symfony.com/doc/2.x/filters/title.html" rel="nofollow noreferrer">Twig's title filter</a>, and this is a good fallback if there isn't anything else.</p>

<users>
How to enforce capitalisation on users First Name and Last Name
# How to enforce capitalisation on users First Name and Last Name

(Asked by: username_0 on 2019-04-12)

We have developed a Craft 3 site that has user registration. Once a user account has been confirmed and activated, they are shown in a Contacts directory, which other users can search through.

As the user is responsible for adding their own details, a few users have submitted their names in lowercase:

`jamie wade` instead of `Jamie Wade`.

Whilst there is nothing *wrong* with this, it doesn't look great. Is there anyway we can enforce capitalisation in Craft on the users First Name and Last Name fields?

I am aware of [Twig's title filter](https://twig.symfony.com/doc/2.x/filters/title.html), and this is a good fallback if there isn't anything else.

**Tags:** users

---

## 1 answers

### Answer by username_1 on 2019-04-12. Score: 2 | Accepted answer

You could easily add client side (JS) validation before the user registration form is submitted. How to do that depends on your JS architecture, I'd use something like vee-validate with a regex validation.
However it would probably be more user friendly to just automatically do the title casing in a custom module, adding something like the following inside the module's init() function:
Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_BEFORE_SAVE_ELEMENT, function(Event $event) {
    if ($event->element instanceof \craft\elements\User) {
        $event->element->firstName = \craft\helpers\StringHelper::titleize($event->element->firstName);
        $event->element->lastName = \craft\helpers\StringHelper::titleize($event->element->lastName);
    }
});

Just beware if you have international users. Who knows what this will do to their names. See Falsehoods programmers believe about names

> **Comments:**
> **username_0**: Thanks Udo, that sounds reasonable. That blog post is also a real eye-opener, thanks for sharing!  

---

