silverstripe-framework/forms/EmailField.php
Sean Harvey 9f3344b355 API CHANGE Removed built-in behaviour.js client-side form validation.
This is no longer supported. Please use custom client-side validation instead. (see 3.0.0 changelog
for more information)
2012-03-09 12:19:57 +13:00

44 lines
1.0 KiB
PHP

<?php
/**
* Text field with Email Validation.
* @package forms
* @subpackage fields-formattedinput
*/
class EmailField extends TextField {
function Type() {
return 'email text';
}
/**
* 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);
$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."),
"validation"
);
return false;
} else{
return true;
}
}
}