mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Ascii rule added
Ascii unit test added
This commit is contained in:
parent
3511ad84af
commit
971da213a3
@ -505,6 +505,24 @@ class Validator
|
||||
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
|
||||
*
|
||||
|
||||
@ -617,6 +617,20 @@ class ValidateTest extends BaseTestCase
|
||||
$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()
|
||||
{
|
||||
$v = new Validator(array('ip' => '127.0.0.1'));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user