Email validation rejects email with special characters
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.
On the subject of email regexs... I've spotted an issue with email validation if
- there's a subdomain
- 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
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());
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.
Emails with special characters still get rejected.
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.
PASS [email protected]
PASS [email protected]
PASS [email protected]
PASS [email protected]
PASS [email protected]
FAIL [email protected]
FAIL [email protected]
FAIL [email protected]
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
punycodelibrary 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!
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