diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 28e61d36c461..0ca644f3e312 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -563,7 +563,18 @@ public function validateDigits($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'digits'); - return ! preg_match('/[^0-9]/', $value) + $length = strlen((string) $value); + + if (((string) $value) === '.') { + return false; + } + + // Make sure there is not more than one dot... + if (($length - strlen(str_replace('.', '', (string) $value))) > 1) { + return false; + } + + return ! preg_match('/[^0-9.]/', $value) && strlen((string) $value) == $parameters[0]; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 95ca15884d9a..61b07f45bb27 100644 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2506,6 +2506,30 @@ public function testValidateDigits() $v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']); $this->assertTrue($v->fails()); + $v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'digits:3']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['foo' => '0.9876'], ['foo' => 'digits:5']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '1..2'], ['foo' => 'digits:4']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '123.456.789'], ['foo' => 'digits:10']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '...'], ['foo' => 'digits:3']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '.'], ['foo' => 'digits:1']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '.2'], ['foo' => 'digits:2']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['foo' => '2.'], ['foo' => 'digits:2']); + $this->assertTrue($v->passes()); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']); $this->assertTrue($v->passes());