2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Text field with Email Validation.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class EmailField extends TextField {
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-01-10 04:28:13 +01:00
|
|
|
$error = _t('EmailField.VALIDATIONJS', 'Please enter an email address.');
|
2007-07-19 12:40:28 +02:00
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateEmailField: function(fieldName) {
|
|
|
|
var el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
2010-10-19 06:57:52 +02:00
|
|
|
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)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(el, "$error","validation");
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
2009-06-08 02:41:06 +02:00
|
|
|
//fix for the problem with more than one form on a page.
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateEmailField' .'_' . $formID);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
//return "\$('$formID').validateEmailField('$this->name');";
|
|
|
|
return <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
$('$formID').validateEmailField('$this->name');
|
|
|
|
}else{
|
|
|
|
$('$formID').validateEmailField('$this->name');
|
|
|
|
}
|
|
|
|
JS;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-19 06:57:52 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function validate($validator){
|
|
|
|
$this->value = trim($this->value);
|
2010-10-19 06:57:52 +02:00
|
|
|
|
|
|
|
$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)){
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t('EmailField.VALIDATION', "Please enter an email address."),
|
|
|
|
"validation"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
} else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|