From 9dc1306439fd4ba12d1ae08c84c75ab878b8a735 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 10 Oct 2010 05:38:29 +0000 Subject: [PATCH] BUGFIX RFC 2822 compliant validation of email adresses in EmailField->jsValidation() and EmailField->validate() (fixes #6067, thanks paradigmincarnate) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@111841 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/EmailField.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/forms/EmailField.php b/forms/EmailField.php index 97530dbc9..5f9dea482 100755 --- a/forms/EmailField.php +++ b/forms/EmailField.php @@ -16,7 +16,7 @@ Behaviour.register({ var el = _CURRENT_FORM.elements[fieldName]; if(!el || !el.value) return true; - if(el.value.match(/^([a-zA-Z0-9_+\.\x27-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)) { + if(el.value.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i)) { return true; } else { validationError(el, "$error","validation"); @@ -40,9 +40,25 @@ if(typeof fromAnOnBlur != 'undefined'){ JS; } + /** + * Validates for RFC 2822 compliant email adresses. + * + * @see http://www.regular-expressions.info/email.html + * @see http://www.ietf.org/rfc/rfc2822.txt + * + * @param Validator $validator + * @return String + */ function validate($validator){ $this->value = trim($this->value); - if($this->value && !ereg('^([a-zA-Z0-9_+\'.-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$', $this->value)){ + + $pcrePattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$'; + + + // PHP uses forward slash (/) to delimit start/end of pattern, so it must be escaped + $pregSafePattern = str_replace('/', '\\/', $pcrePattern); + + if($this->value && !preg_match('/' . $pregSafePattern . '/i', $this->value)){ $validator->validationError( $this->name, _t('EmailField.VALIDATION', "Please enter an email address."),