diff --git a/README.md b/README.md index 4da73cb..ca9d986 100644 --- a/README.md +++ b/README.md @@ -133,12 +133,15 @@ V::lang('ar'); * `in` - Performs in_array check on given array values * `notIn` - Negation of `in` rule (not in array of values) * `ip` - Valid IP address + * `ipv4` - Valid IP v4 address + * `ipv6` - Valid IP v6 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 * `alphaNum` - Alphabetic and numeric characters only + * `ascii` - ASCII characters only * `slug` - URL slug characters (a-z, 0-9, -, \_) * `regex` - Field matches given regex pattern * `date` - Field is a valid date diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 5d44548..c9fe2f9 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -493,6 +493,30 @@ class Validator return filter_var($value, \FILTER_VALIDATE_IP) !== false; } + /** + * Validate that a field is a valid IP v4 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv4($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false; + } + + /** + * Validate that a field is a valid IP v6 address + * + * @param string $field + * @param mixed $value + * @return bool + */ + protected function validateIpv6($field, $value) + { + return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false; + } + /** * Validate that a field is a valid e-mail address * @@ -505,6 +529,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 * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 3233245..614302b 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -621,6 +621,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')); @@ -635,6 +649,34 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testIpv4Valid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv4', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv4Invalid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv4', 'ip'); + $this->assertFalse($v->validate()); + } + + public function testIpv6Valid() + { + $v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329')); + $v->rule('ipv6', 'ip'); + $this->assertTrue($v->validate()); + } + + public function testIpv6Invalid() + { + $v = new Validator(array('ip' => '127.0.0.1')); + $v->rule('ipv6', 'ip'); + $this->assertFalse($v->validate()); + } + public function testEmailValid() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));