From 62a4d0a39166df5bcb9c9a89431b32ec6fab8acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 6 Feb 2012 00:10:24 +0100 Subject: [PATCH] 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*. --- includes/form/package.inc.php | 1 + includes/form/validators.inc.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/includes/form/package.inc.php b/includes/form/package.inc.php index 458cf18..470d331 100644 --- a/includes/form/package.inc.php +++ b/includes/form/package.inc.php @@ -22,3 +22,4 @@ $package[ 'extras' ][] = 'Modifier_TrimString'; $package[ 'extras' ][] = 'Validator_StringLength'; $package[ 'extras' ][] = 'Validator_InArray'; $package[ 'extras' ][] = 'Validator_IntValue'; +$package[ 'extras' ][] = 'Validator_Email'; diff --git a/includes/form/validators.inc.php b/includes/form/validators.inc.php index ac39f24..b1be026 100644 --- a/includes/form/validators.inc.php +++ b/includes/form/validators.inc.php @@ -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 ); + } +}