Skip to content

Commit daa7f85

Browse files
author
Mattias Grenhall
committed
fix: Email regex update to support special characters
1 parent d91478c commit daa7f85

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

packages/payload/src/fields/email.ts

Whitespace-only changes.

packages/payload/src/fields/validations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const email: EmailFieldValidation = (
200200
* Supports multiple subdomains (e.g., user@sub.domain.example.com)
201201
*/
202202
const emailRegex =
203-
/^(?!.*\.\.)[\w.%+-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i
203+
/^(?!.*\.\.)[\w!#$%&'*+/=?^`{|}~.-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i
204204

205205
if ((value && !emailRegex.test(value)) || (!value && required)) {
206206
return t('validation:emailAddress')

test/auth/int.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ describe('Auth', () => {
10161016
expect(emailValidation('user.name+alias@example.co.uk', mockContext)).toBe(true)
10171017
expect(emailValidation('user-name@example.org', mockContext)).toBe(true)
10181018
expect(emailValidation('user@ex--ample.com', mockContext)).toBe(true)
1019+
expect(emailValidation("user'payload@example.org", mockContext)).toBe(true)
10191020
})
10201021

10211022
it('should not allow emails with double quotes', () => {
@@ -1045,5 +1046,11 @@ describe('Auth', () => {
10451046
expect(emailValidation('user@-example.com', mockContext)).toBe('validation:emailAddress')
10461047
expect(emailValidation('user@example-.com', mockContext)).toBe('validation:emailAddress')
10471048
})
1049+
it('should not allow emails that start with dot', () => {
1050+
expect(emailValidation('.user@example.com', mockContext)).toBe('validation:emailAddress')
1051+
})
1052+
it('should not allow emails that have a comma', () => {
1053+
expect(emailValidation('user,name@example.com', mockContext)).toBe('validation:emailAddress')
1054+
})
10481055
})
10491056
})

0 commit comments

Comments
 (0)