# Django Form: error Enter a number

**URL:** <https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115>\
**Category:** Templates & Frontend\
**Created:** [February 25, 2023, 7:51pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115 "2023-02-25T19:51:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 25, 2023, 7:51pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/1 "2023-02-25T19:51:18Z")

</div>

Hi

I have a form where user can enter float or int  
like, 1000 or 1,000.10. the problem when submit the form with a `float` it raises error `Enter a number`

```auto
 asset = models.FloatField( default=0, verbose_name=_("Total Assest")) 

```

```auto
<input 
type="number" 
name="{{form.asset.html_name}}" 
id="{{form.asset.id_for_label}}" 
class="form-control 
{% if form.asset.errors %}is-invalid{% endif %}" 
value="{{form.asset.value|unlocalize|default_if_none:''
|floatformat:2|intcomma}}" 
step="0.01"> 

```

settings

```auto
USE_I18N = True

USE_TZ = True

USE_L10N = True

USE_THOUSAND_SEPARATOR = True

```

i need to let the user enter int or float.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 25, 2023, 8:12pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/2 "2023-02-25T20:12:40Z")

</div>

What does your form field look like for this form?

Are you getting this error in server-side validation or from the browser?  
(Note that the thousand’s separator is _not_ considered part of a numeric value. It is not legal to enter 1,000.10, you would need to enter it as 1000.10. If you need “1,000.10” to be accepted as a valid entry, you need to find a JavaScript widget to support that.)

See [\<input type="number"\> - HTML: HyperText Markup Language | MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number)

---

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 25, 2023, 9:44pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/3 "2023-02-25T21:44:44Z")

</div>

The error is in front-end.

That’s a good note about `USE_THOUSAND_SEPARATOR`.

_I need a way to let the user add float or int in the same input._

at /edit/ values are shown to user like 1,000.00 but if he try to save the form, it raises front-end error `Enter a number.`

values without ‘,’ are excepted and the form can be updated. (user must delete the `THOUSAND_SEPARATOR` which are send from the back-end )

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 26, 2023, 12:39am UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/4 "2023-02-26T00:39:53Z")

</div>

If you need to allow the commas as input, then you need to change the field to be a CharField and not a numeric field. You can then use JavaScript to perform front-end verification of the entered field along with performing the validation in your view.

---

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 26, 2023, 1:14pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/5 "2023-02-26T13:14:07Z")

</div>

i think the beat way is to remove the ‘,’ before save the form. My model is `asset = models.FloatField`

I fount this question, but i can’t remove the ‘,’

> <https://stackoverflow.com/questions/64888685/django-form-comma-separated-decimal-numbers-throwing-validation-errors>

```auto
class ReportUpdateForm(forms.ModelForm):
    year = forms.ModelChoiceField(queryset=Year.objects.none(), disabled=True)

    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        self.fields['year'].queryset = Year.objects.filter(name__gte=self.instance.org.since)

# below is me trying to delete the ',' before saving the form
        
    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        self.fields['asset', 'donation', 'expense']
            print('fields', self.fields)
            return float(self.fields.replace(',', ''))

# or
    def clean_(self):
        fields = self.cleaned_data['asset', 'donation', 'expense']
        print('fields', fields)
        return float(fields.replace(',', ''))
            

    class Meta:
        model = Report
        fields = ['asset', 'donation', 'expense', 'year']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 26, 2023, 2:45pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/6 "2023-02-26T14:45:14Z")

</div>

When you’re getting this error, is it posting the form to the server? (Do you see the POST in your console log?)

---

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 26, 2023, 3:03pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/7 "2023-02-26T15:03:37Z")

</div>

If insert 35,000.00 that shows error `Enter a number.`  
Nothing is saved to DB and there is no error in the logs or console

![Enter a number error](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/5/51072e8b5f7fc6c331a45f9fa48fde19d226622d.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 26, 2023, 3:19pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/8 "2023-02-26T15:19:37Z")

</div>

So, if it’s not posting back to the server, why do you think that making any changes in the view is going to help this situation? Nothing is being submitted, no code in the server is going to execute.

> [@Django Form: error Enter a number](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/4):
>
> If you need to allow the commas as input, then you need to change the field to be a CharField and not a numeric field. You can then use JavaScript to perform front-end verification of the entered field along with performing the validation in your view.

---

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 26, 2023, 3:25pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/9 "2023-02-26T15:25:44Z")

</div>

i can’t use `CharField`

i need a way to remove the ‘,’ so if user insert **35,000.00** that well saved as **35000.00**

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 26, 2023, 3:29pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/10 "2023-02-26T15:29:18Z")

</div>

> [@xMrAli](#):
>
> i can’t use `CharField`

Why not?

To be clear, I’m talking about `forms.CharField` (or the `TextInput` widget, or `type="text"`), _not_ `models.CharField`.

Again, your issue here is _not_ with “trying to save the comma”. Your _immediate_ problem is that “35,000” is not a valid `type="number"` input in your input field. You’re not even at the point where it’s trying to save this to the database.

Side note: I sincerely hope you’re not trying to use a FloatField for a monetary value. That’s a _huge_ mistake, and the reason why Django (and databases) provide a DecimalField.

---

<div class="post-metadata">

**Author:** ![xMrAli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/xmrali/32/8206_2.png) [@xMrAli](https://forum.djangoproject.com/u/xMrAli)\
**Post date:** [February 26, 2023, 3:45pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/11 "2023-02-26T15:45:19Z")

</div>

OK well do `models.DecimalField` it’s monetary values.There are many math functions for this field.

my _immediate_ problem is that “35,000” is not a valid `type="number"` input in the input field.

Q. how can i remove the comma _if user_ add it, Because without comma “35000.00”, input well accept and save.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 26, 2023, 3:46pm UTC](https://forum.djangoproject.com/t/django-form-error-enter-a-number/19115/12 "2023-02-26T15:46:20Z")

</div>

I’ve already answered this. See my previous answers above.
