Create custom validation for names

I had to use a custom validation because we want to different
error message for each part of validation.
I didn't find a better solution for Parsley library.

[MAILPOET-3786]
This commit is contained in:
Jan Lysý
2021-09-14 09:37:12 +02:00
committed by Veljko V
parent ec86e742c2
commit f9ba4623c6
2 changed files with 37 additions and 4 deletions

View File

@@ -1,11 +1,34 @@
import MailPoet from 'mailpoet';
import jQuery from 'jquery';
import Cookies from 'js-cookie';
import 'parsleyjs';
import Parsley from 'parsleyjs';
const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent';
jQuery(($) => {
Parsley.addValidator('names', {
requirementType: ['string', 'string'],
validateString: (value, errorBrackets, errorURL) => {
// Name can't contain angle brackets - https://mailpoet.atlassian.net/browse/MAILPOET-3408
const bracketsExpression = /[><]+/gi;
const bracketsRegex = new RegExp(bracketsExpression);
if (value.match(bracketsRegex)) {
return $.Deferred().reject(errorBrackets);
}
// Name can't contain URL - https://mailpoet.atlassian.net/browse/MAILPOET-3786
const urlExpression = /https?:\/\/(www\.)?(.+)\.(.+)/gi;
const urlRegex = new RegExp(urlExpression);
if (value.match(urlRegex)) {
return $.Deferred().reject(errorURL);
}
return true;
},
messages: {
en: 'Please specify a valid name',
},
});
function renderCaptcha(element, iteration) {
if (!window.recaptcha || !window.grecaptcha.ready) {
if (iteration < 20) {