Skip to content
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

Relaxing the email validation #1735

Merged
merged 5 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 29 additions & 0 deletions src/__tests__/field/email.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isEmail, emailDomain } from '../../field/email';

describe('field/email', () => {
describe('isEmail', () => {
it('validates correctly', () => {
expect(isEmail('test@test.com')).toBe(true);
expect(isEmail('test@test.com.br')).toBe(true);
expect(isEmail('test@移动.移动')).toBe(true);
expect(isEmail('test@test.com ')).toBe(true);
expect(isEmail(' test@test.com')).toBe(true);
expect(isEmail(1)).toBe(false);
expect(isEmail('test@testcom')).toBe(false);
expect(isEmail('test.test.com')).toBe(false);
});
it('returns false when there is a white space in the middle of the string', () => {
expect(isEmail('test@test. com')).toBe(false);
});
});
describe('emailDomain', () => {
it('extracts email domain correctly', () => {
expect(emailDomain('test@test.com')).toBe('test.com');
expect(emailDomain('test@test.com.br')).toBe('test.com.br');
expect(emailDomain('test@移动.移动')).toBe('移动.移动');
expect(emailDomain(1)).toBe('');
expect(emailDomain('test@testcom')).toBe('');
expect(emailDomain('test.test.com')).toBe('');
});
});
});
17 changes: 10 additions & 7 deletions src/field/email.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import trim from 'trim';

import { setField } from './index';
import { endsWith } from '../utils/string_utils';
import { isHRDEmailValid } from '../connection/enterprise';
import * as i18n from '../i18n';

const regExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export function validateEmail(str) {
return isEmail(str);
}

export function isEmail(str) {
const result = regExp.exec(trim(str.toLowerCase()));
luisrudge marked this conversation as resolved.
Show resolved Hide resolved
return !!result && result[0] !== null;
if (typeof str !== 'string') {
return false;
}
const trimmed = trim(str);
return trimmed.indexOf('@') >= 0 && trimmed.indexOf('.') >= 0 && trimmed.indexOf(' ') === -1;
}

export function setEmail(m, str) {
Expand All @@ -27,8 +28,10 @@ export function setEmail(m, str) {
}

export function emailDomain(str) {
const result = regExp.exec(trim(str.toLowerCase()));
return result ? result.slice(-2)[0] : '';
if (!isEmail(str)) {
return '';
}
return str.split('@')[1].toLowerCase();
}

export function emailLocalPart(str) {
Expand Down
3 changes: 2 additions & 1 deletion src/ui/input/email_input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export default class EmailInput extends React.Component {
<input
id={`${lockId}-email`}
ref="input"
type="email"
type="text"
inputMode="email"
name="email"
className="auth0-lock-input"
placeholder="yours@example.com"
Expand Down