Skip to content

fix: update email regex to support special characters #12181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/payload/src/fields/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const email: EmailFieldValidation = (
* Supports multiple subdomains (e.g., user@sub.domain.example.com)
*/
const emailRegex =
/^(?!.*\.\.)[\w.%+-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i
/^(?!.*\.\.)[\w!#$%&'*+/=?^`{|}~.-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i

if ((value && !emailRegex.test(value)) || (!value && required)) {
return t('validation:emailAddress')
Expand Down
7 changes: 7 additions & 0 deletions test/auth/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@
expect(emailValidation('user.name+alias@example.co.uk', mockContext)).toBe(true)
expect(emailValidation('user-name@example.org', mockContext)).toBe(true)
expect(emailValidation('user@ex--ample.com', mockContext)).toBe(true)
expect(emailValidation("user'payload@example.org", mockContext)).toBe(true)
})

it('should not allow emails with double quotes', () => {
Expand Down Expand Up @@ -1045,5 +1046,11 @@
expect(emailValidation('user@-example.com', mockContext)).toBe('validation:emailAddress')
expect(emailValidation('user@example-.com', mockContext)).toBe('validation:emailAddress')
})
it('should not allow emails that start with dot', () => {
expect(emailValidation('.user@example.com', mockContext)).toBe('validation:emailAddress')

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-mongodb

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres-custom-schema

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-postgres-uuid

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-supabase

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)

Check failure on line 1050 in test/auth/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite-uuid

Auth › Email - format validation › should not allow emails that start with dot

expect(received).toBe(expected) // Object.is equality Expected: "validation:emailAddress" Received: true at Object.toBe (auth/int.spec.ts:1050:65)
})
it('should not allow emails that have a comma', () => {
expect(emailValidation('user,name@example.com', mockContext)).toBe('validation:emailAddress')
})
})
})
Loading