diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index 57b211be5efe..cefe16469b5b 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -44,7 +44,8 @@ public function alpha_space(?string $value = null): bool return true; } - return (bool) preg_match('/^[A-Z ]+$/i', $value); + // @see https://regex101.com/r/LhqHPO/1 + return (bool) preg_match('/\A[A-Z ]+\z/i', $value); } /** @@ -56,7 +57,8 @@ public function alpha_space(?string $value = null): bool */ public function alpha_dash(?string $str = null): bool { - return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); + // @see https://regex101.com/r/XfVY3d/1 + return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str); } /** @@ -72,7 +74,8 @@ public function alpha_dash(?string $str = null): bool */ public function alpha_numeric_punct($str) { - return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str); + // @see https://regex101.com/r/6N8dDY/1 + return (bool) preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str); } /** @@ -96,7 +99,8 @@ public function alpha_numeric(?string $str = null): bool */ public function alpha_numeric_space(?string $str = null): bool { - return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str); + // @see https://regex101.com/r/0AZDME/1 + return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str); } /** @@ -123,7 +127,8 @@ public function string($str = null): bool */ public function decimal(?string $str = null): bool { - return (bool) preg_match('/^[-+]?[0-9]{0,}\.?[0-9]+$/', $str); + // @see https://regex101.com/r/HULifl/1/ + return (bool) preg_match('/\A[-+]?[0-9]{0,}\.?[0-9]+\z/', $str); } /** @@ -147,7 +152,7 @@ public function hex(?string $str = null): bool */ public function integer(?string $str = null): bool { - return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); + return (bool) preg_match('/\A[\-+]?[0-9]+\z/', $str); } /** @@ -181,7 +186,8 @@ public function is_natural_no_zero(?string $str = null): bool */ public function numeric(?string $str = null): bool { - return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); + // @see https://regex101.com/r/bb9wtr/1 + return (bool) preg_match('/\A[\-+]?[0-9]*\.?[0-9]+\z/', $str); } /** @@ -253,6 +259,7 @@ public function valid_json(string $str = null): bool */ public function valid_email(string $str = null): bool { + // @see https://regex101.com/r/wlJG1t/1/ if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches)) { $str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46); diff --git a/tests/system/Validation/FormatRulesTest.php b/tests/system/Validation/FormatRulesTest.php index d7b171fe098a..56145c703201 100644 --- a/tests/system/Validation/FormatRulesTest.php +++ b/tests/system/Validation/FormatRulesTest.php @@ -424,6 +424,10 @@ public function alphaSpaceProvider() FormatRulesTest::ALPHABET . ' ', true, ], + [ + FormatRulesTest::ALPHABET . "\n", + false, + ], [ FormatRulesTest::ALPHABET . '1', false, @@ -506,6 +510,10 @@ public function alphaNumericPunctProvider() FormatRulesTest::ALPHANUMERIC . '`', false, ], + [ + FormatRulesTest::ALPHANUMERIC . "\n", + false, + ], [ FormatRulesTest::ALPHANUMERIC . '@', false, @@ -599,6 +607,10 @@ public function alphaNumericSpaceProvider() ' ' . FormatRulesTest::ALPHANUMERIC . '-', false, ], + [ + ' ' . FormatRulesTest::ALPHANUMERIC . "\n", + false, + ], [ null, false, @@ -636,6 +648,10 @@ public function alphaDashProvider() FormatRulesTest::ALPHANUMERIC . '-\ ', false, ], + [ + FormatRulesTest::ALPHANUMERIC . "-\n", + false, + ], [ null, false, @@ -722,6 +738,10 @@ public function numericProvider() '+42', true, ], + [ + "+42\n", + false, + ], [ '123a', false, @@ -771,6 +791,10 @@ public function integerProvider() '-1', true, ], + [ + "+42\n", + false, + ], [ '123a', false, @@ -824,6 +848,10 @@ public function decimalProvider() '0', true, ], + [ + "0\n", + false, + ], [ '1.0a', false,