E-mail validator

Added an e-mail validator to the form package. The validator is really
primitive and will only accept a small subset of email addresses, but I
needed one *now*.
This commit is contained in:
Emmanuel BENOîT 2012-02-06 00:10:24 +01:00
parent f80d739f5d
commit 62a4d0a391
2 changed files with 24 additions and 0 deletions

View file

@ -22,3 +22,4 @@ $package[ 'extras' ][] = 'Modifier_TrimString';
$package[ 'extras' ][] = 'Validator_StringLength'; $package[ 'extras' ][] = 'Validator_StringLength';
$package[ 'extras' ][] = 'Validator_InArray'; $package[ 'extras' ][] = 'Validator_InArray';
$package[ 'extras' ][] = 'Validator_IntValue'; $package[ 'extras' ][] = 'Validator_IntValue';
$package[ 'extras' ][] = 'Validator_Email';

View file

@ -99,3 +99,26 @@ class Validator_IntValue
} }
} }
} }
class Validator_Email
implements FieldValidator
{
private $errorText;
public function __construct( $errorText )
{
$this->errorText = $errorText;
}
public function validate( $value )
{
if ( preg_match( "/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/" , $value ) ) {
list( $username , $domain ) = split( '@' , $value );
if ( checkdnsrr( $domain , 'MX' ) ) {
return null;
}
}
return array( $this->errorText );
}
}