diff --git a/django/forms/fields.py b/django/forms/fields.py index c06352a964..7a59a3d664 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -279,6 +279,15 @@ class FloatField(IntegerField): raise ValidationError(self.error_messages['invalid'], code='invalid') return value + def validate(self, value): + super(FloatField, self).validate(value) + + # Check for NaN (which is the only thing not equal to itself) and +/- infinity + if value != value or value in (Decimal('Inf'), Decimal('-Inf')): + raise ValidationError(self.error_messages['invalid'], code='invalid') + + return value + def widget_attrs(self, widget): attrs = super(FloatField, self).widget_attrs(widget) if isinstance(widget, NumberInput): diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index a4e81d31ae..6f305d78c4 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -254,6 +254,9 @@ class FieldsTests(SimpleTestCase): self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '1.0a') self.assertEqual(f.max_value, None) self.assertEqual(f.min_value, None) + self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, 'Infinity') + self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, 'NaN') + self.assertRaisesMessage(ValidationError, "'Enter a number.'", f.clean, '-Inf') def test_floatfield_2(self): f = FloatField(required=False)