Refactor JS email regexp to be accessible from every page MAILPOET-1288

This commit is contained in:
Rostislav Wolny
2018-03-01 09:17:53 +01:00
parent c164066522
commit 449568b37b
4 changed files with 11 additions and 11 deletions

View File

@@ -209,7 +209,7 @@ define(
for (column in rowData) { for (column in rowData) {
emailAddress = detectAndCleanupEmail(rowData[column]); emailAddress = detectAndCleanupEmail(rowData[column]);
if (emailColumnPosition === null if (emailColumnPosition === null
&& window.emailRegex.test(emailAddress)) { && window.mailpoet_email_regex.test(emailAddress)) {
emailColumnPosition = column; emailColumnPosition = column;
// add current e-mail to an object index // add current e-mail to an object index
parsedEmails[emailAddress] = true; parsedEmails[emailAddress] = true;
@@ -229,7 +229,7 @@ define(
if (_.has(parsedEmails, email)) { if (_.has(parsedEmails, email)) {
duplicateEmails.push(email); duplicateEmails.push(email);
} }
else if (!window.emailRegex.test(email)) { else if (!window.mailpoet_email_regex.test(email)) {
invalidEmails.push(rowData[emailColumnPosition]); invalidEmails.push(rowData[emailColumnPosition]);
} }
// if we haven't yet processed this e-mail and it passed // if we haven't yet processed this e-mail and it passed
@@ -257,7 +257,7 @@ define(
// since we assume that the header line is always present, we need // since we assume that the header line is always present, we need
// to detect the header by checking if it contains a valid e-mail address // to detect the header by checking if it contains a valid e-mail address
window.importData.step1 = { window.importData.step1 = {
header: (!window.emailRegex.test( header: (!window.mailpoet_email_regex.test(
processedSubscribers[0][emailColumnPosition]) processedSubscribers[0][emailColumnPosition])
) ? processedSubscribers.shift() : null, ) ? processedSubscribers.shift() : null,
subscribers: processedSubscribers, subscribers: processedSubscribers,
@@ -690,7 +690,7 @@ define(
columnData = helperSubscribers.subscribers[0][i]; columnData = helperSubscribers.subscribers[0][i];
columnId = 'ignore'; // set default column type columnId = 'ignore'; // set default column type
// if the column is not undefined and has a valid e-mail, set type as email // if the column is not undefined and has a valid e-mail, set type as email
if (columnData % 1 !== 0 && window.emailRegex.test(columnData)) { if (columnData % 1 !== 0 && window.mailpoet_email_regex.test(columnData)) {
columnId = 'email'; columnId = 'email';
} else if (helperSubscribers.header) { } else if (helperSubscribers.header) {
headerName = helperSubscribers.header[i]; headerName = helperSubscribers.header[i];
@@ -791,7 +791,9 @@ define(
// EMAIL filter: if the first value in the column doesn't have a valid // EMAIL filter: if the first value in the column doesn't have a valid
// email, hide the next button // email, hide the next button
if (column.id === 'email') { if (column.id === 'email') {
if (!window.emailRegex.test(subscribersClone.subscribers[0][matchedColumn.index])) { if (!window.mailpoet_email_regex.test(
subscribersClone.subscribers[0][matchedColumn.index])
) {
preventNextStep = true; preventNextStep = true;
if (!jQuery('[data-id="notice_invalidEmail"]').length) { if (!jQuery('[data-id="notice_invalidEmail"]').length) {
MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidElement'), { MailPoet.Notice.error(MailPoet.I18n.t('columnContainsInvalidElement'), {

View File

@@ -48,6 +48,8 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
var mailpoet_premium_version = <%= json_encode(mailpoet_premium_version()) %>; var mailpoet_premium_version = <%= json_encode(mailpoet_premium_version()) %>;
var mailpoet_analytics_enabled = <%= is_analytics_enabled() | json_encode %>; var mailpoet_analytics_enabled = <%= is_analytics_enabled() | json_encode %>;
var mailpoet_analytics_data = <%= json_encode(get_analytics_data()) %>; var mailpoet_analytics_data = <%= json_encode(get_analytics_data()) %>;
// RFC 5322 standard; http://emailregex.com/
var mailpoet_email_regex = /^(([^<>()\[\]\\.,;:\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,}))$/;
</script> </script>
<!-- javascripts --> <!-- javascripts -->

View File

@@ -59,8 +59,6 @@
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
// RFC 5322 standard; http://emailregex.com/
emailRegex = /^(([^<>()\[\]\\.,;:\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,}))$/;
jQuery(function($) { jQuery(function($) {
// on dom loaded // on dom loaded
$(function() { $(function() {
@@ -68,7 +66,7 @@
$('#mailpoet_settings_form').on('submit', function() { $('#mailpoet_settings_form').on('submit', function() {
// Check if filled emails are valid // Check if filled emails are valid
var invalidEmails = $.map($('#mailpoet_settings_form')[0].elements, function(el) { var invalidEmails = $.map($('#mailpoet_settings_form')[0].elements, function(el) {
return el.type === 'email' && el.value && !window.emailRegex.test(el.value) ? el.value : null; return el.type === 'email' && el.value && !window.mailpoet_email_regex.test(el.value) ? el.value : null;
}).filter(function(val) { return !!val; }); }).filter(function(val) { return !!val; });
if (invalidEmails.length) { if (invalidEmails.length) {
MailPoet.Notice.error( MailPoet.Notice.error(

View File

@@ -24,9 +24,7 @@
importData = {}, importData = {},
mailpoetColumnsSelect2 = <%= subscriberFieldsSelect2|raw %>, mailpoetColumnsSelect2 = <%= subscriberFieldsSelect2|raw %>,
mailpoetColumns = <%= subscriberFields|raw %>, mailpoetColumns = <%= subscriberFields|raw %>,
mailpoetSegments = <%= segments|raw %>, mailpoetSegments = <%= segments|raw %>
// RFC 5322 standard; http://emailregex.com/
emailRegex = /^(([^<>()\[\]\\.,;:\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,}))$/;
</script> </script>
<% endblock %> <% endblock %>