Ascii rule added

Ascii unit test added
This commit is contained in:
peterkinamo 2018-08-03 12:28:23 +02:00
parent 3511ad84af
commit 971da213a3
2 changed files with 32 additions and 0 deletions

View File

@ -505,6 +505,24 @@ class Validator
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false; return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false;
} }
/**
* Validate that a field contains only ASCII characters
*
* @param $field
* @param $value
* @return bool|false|string
*/
protected function validateAscii($field, $value)
{
// multibyte extension needed
if (function_exists('mb_detect_encoding')) {
return mb_detect_encoding($value, 'ASCII', true);
}
// fallback with regex
return 0 === preg_match('/[^\x00-\x7F]/', $value);
}
/** /**
* Validate that a field is a valid e-mail address and the domain name is active * Validate that a field is a valid e-mail address and the domain name is active
* *

View File

@ -617,6 +617,20 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testAsciiValid()
{
$v = new Validator(array('text' => '12345 abcde'));
$v->rule('ascii', 'text');
$this->assertTrue($v->validate());
}
public function testAsciiInvalid()
{
$v = new Validator(array('text' => '12345 abcdé'));
$v->rule('ascii', 'text');
$this->assertFalse($v->validate());
}
public function testIpValid() public function testIpValid()
{ {
$v = new Validator(array('ip' => '127.0.0.1')); $v = new Validator(array('ip' => '127.0.0.1'));