Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kylemilloy's avatar

Email validation with TLD

Email validation for Laravel allows emails without TLDs...which matches the email RFC...however this breaks if I try to pass an email like example@email (which is technically valid) to a service like Stripe. Is there a "laravel" way to enforce an email has a TLD or is my best bet just to do a regex validator.

0 likes
4 replies
JohnBraun's avatar

I think your best bet is making a custom validation rule.

php artisan make:rule EmailMustHaveTLD

EmailMustHaveTLD.php

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class EmailMustHaveTLD implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (! strpos($value, '@')) {
            return;
        }

        $domain = explode('@', $value)[1];

        $hasTLD = !! strpos($domain, '.');

        return $hasTLD;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'E-mail must have a TLD';
    }
}

In your controller:

use App\Rules\EmailMustHaveTLD;

public function store(Request $request)
{
    $request->validate([
      // ...
      'email' => ['required', 'email', new EmailMustHaveTLD],
      //...
  ]);
}
kylemilloy's avatar

Yeah this is what I had already done in addition to registering the Rule with Validator in a Service Provider...was hoping there'd be a nicer way out of the box

Please or to participate in this conversation.