Skip to content

Commit

Permalink
Moved email validation to helper, changed logic for checking emails
Browse files Browse the repository at this point in the history
  • Loading branch information
dhochbaum-dcp committed Dec 12, 2024
1 parent 7c71762 commit 97221a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
19 changes: 2 additions & 17 deletions client/app/components/subscription-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { action, computed, set } from '@ember/object';
import fetch from 'fetch';
import ENV from 'labs-zap-search/config/environment';
import { getCommunityDistrictsByBorough } from '../helpers/lookup-community-district';
import { validateEmail } from '../helpers/validate-email';

export default class SubscriptionFormComponent extends Component {
communityDistrictsByBorough = {};
Expand Down Expand Up @@ -62,23 +63,7 @@ export default class SubscriptionFormComponent extends Component {

@computed('args.email')
get isEmailValid() {
// eslint-disable-next-line no-useless-escape
const tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
if (!this.args.email) return false;

if (this.args.email.length > 254) return false;

const valid = tester.test(this.args.email);
if (!valid) return false;

// Further checking of some things regex can't handle
const parts = this.args.email.split('@');
if (parts[0].length > 64) return false;

const domainParts = parts[1].split('.');
if (domainParts.some(function(part) { return part.length > 63; })) return false;

return true;
return validateEmail(this.args.email);
}

@computed('args.subscriptions')
Expand Down
5 changes: 2 additions & 3 deletions client/app/controllers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Controller from '@ember/controller';
import { action, computed } from '@ember/object';
import fetch from 'fetch';
import ENV from 'labs-zap-search/config/environment';

const tlds = ['.com', '.gov', '.edu', '.net', '.org'];
import { validateEmail } from '../helpers/validate-email';

export default class SubscribeController extends Controller {
lastEmailChecked = '';
Expand Down Expand Up @@ -60,7 +59,7 @@ export default class SubscribeController extends Controller {

@action
continuouslyCheckEmail(event) {
if ((this.startContinuouslyChecking) || (tlds.includes(event.target.value.slice(-4)))) { this.checkExistingEmail(event); }
if ((this.startContinuouslyChecking) || (validateEmail(event.target.value))) { this.checkExistingEmail(event); }
}

@action
Expand Down
24 changes: 24 additions & 0 deletions client/app/helpers/validate-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { helper } from '@ember/component/helper';


export function validateEmail(email) {
// eslint-disable-next-line no-useless-escape
const tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
if (!email) return false;

if (email.length > 254) return false;

const valid = tester.test(email);
if (!valid) return false;

// Further checking of some things regex can't handle
const parts = email.split('@');
if (parts[0].length > 64) return false;

const domainParts = parts[1].split('.');
if (domainParts.some(function(part) { return part.length > 63; })) return false;

return true;
}

export default helper(validateEmail);

0 comments on commit 97221a4

Please sign in to comment.