mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #232 from jabarihunt/improved-email-validator
Add emailDNS validator
This commit is contained in:
commit
ef412d1160
@ -121,6 +121,7 @@ V::lang('ar');
|
||||
* `notIn` - Negation of `in` rule (not in array of values)
|
||||
* `ip` - Valid IP address
|
||||
* `email` - Valid email address
|
||||
* `emailDNS` - Valid email address with active DNS record
|
||||
* `url` - Valid URL
|
||||
* `urlActive` - Valid URL with active DNS record
|
||||
* `alpha` - Alphabetic characters only
|
||||
|
||||
@ -500,6 +500,26 @@ class Validator
|
||||
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address and the domain name is active
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEmailDNS($field, $value)
|
||||
{
|
||||
if ($this->validateEmail($field, $value)) {
|
||||
$domain = ltrim(stristr($value, '@'), '@') . '.';
|
||||
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46')) {
|
||||
$domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
|
||||
}
|
||||
return checkdnsrr($domain, 'ANY');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid URL by syntax
|
||||
*
|
||||
|
||||
@ -560,6 +560,18 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testEmailDnsValid(){
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||
$v->rule('emailDNS', 'email');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testEmailDnsInvalid(){
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx'));
|
||||
$v->rule('emailDNS', 'email');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testUrlValid()
|
||||
{
|
||||
$v = new Validator(array('website' => 'http://google.com'));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user