zod icon indicating copy to clipboard operation
zod copied to clipboard

Email validation rejects email with special characters

Open timheerwagen opened this issue 2 years ago • 6 comments

For example info@räucher.de gets rejected because of the "ä".

In Chrome, special characters are automatically converted to ASCII on the client side and back. However, this is not the case with Firefox and Safari. So I have no problems in Chrome browsers, but in the others.

Im using simple z.string().email(), dont want to transform each to ASCII by myself and use my own validator.

timheerwagen avatar Mar 17 '23 08:03 timheerwagen

On the subject of email regexs... I've spotted an issue with email validation if

  1. there's a subdomain
  2. the domain is hyphenated
const a = z.string().email();
a.parse("[email protected]");

returns

ZodError: [
  {
    "validation": "email",
    "code": "invalid_string",
    "message": "Invalid email",
    "path": []
  }
]

Using Zod 3.21.4

Edit - looks like there's a possible fix proposed https://github.com/colinhacks/zod/pull/2157

MerlinMason avatar Mar 17 '23 12:03 MerlinMason

This is my current workaround:

import { z } from "zod";
import punycode from "punycode";

export const zEmail = z
  .string()
  .transform(punycode.toASCII)
  .pipe(z.string().email());

timheerwagen avatar Mar 21 '23 21:03 timheerwagen

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 19 '23 22:06 stale[bot]

Emails with special characters still get rejected.

kasbah avatar Jul 07 '23 17:07 kasbah

EDIT: I just updated to latest (3.22.4) and that fixed this.

I have a similar issue with emails that contain multiple subdomains with the first level subdomain (closest to the domain) containing numbers.

[email protected]

PASS [email protected]
PASS [email protected]
PASS [email protected]
PASS [email protected]
PASS [email protected]

FAIL [email protected]
FAIL [email protected]
FAIL [email protected]

Soviut avatar Oct 31 '23 00:10 Soviut

Hi, @timheerwagen. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • The issue involves email validation problems with special characters, particularly in Firefox and Safari.
  • You shared a workaround using the punycode library to transform emails to ASCII before validation.
  • @MerlinMason identified related issues with subdomains and hyphenated domains and mentioned a potential fix in a pull request.
  • @kasbah noted that emails with special characters are still being rejected.
  • @Soviut reported similar issues with subdomains containing numbers, which were resolved in Zod version 3.22.4.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of Zod. If so, feel free to keep the discussion open by commenting here.
  • If there is no further activity, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jun 18 '25 16:06 dosubot[bot]

Hi @timheerwagen! I'm Dosu and I’m helping the zod team.

Your punycode workaround is necessary with Zod's default .email() validator, since it only supports ASCII and doesn't natively handle internationalized domain names (IDNs) or Unicode in the domain part (docs, regex source).

If you want to support Unicode emails directly, Zod exports a unicodeEmail regex you can use with a custom pattern:

import { z, regexes } from "zod";

export const zUnicodeEmail = z.string().regex(regexes.unicodeEmail, { message: "Invalid email" });

Or, in Zod 4, you can use:

import { z, regexes } from "zod";

export const zUnicodeEmail = z.email({ pattern: regexes.unicodeEmail });

This will allow Unicode in both the local and domain parts, but be aware it's a much looser check than the default. More details are in the Zod email docs.

If this answers your question, please close the issue!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar Jul 22 '25 03:07 dosubot[bot]