Django Form: Enter a valid number error

Hello,

I’m experiencing a very similar issue to this post, except that my input field is of type text.

I have a form where the user can enter an integer value, such as 1000, which I format using toLocaleString('en-US') before submission.

  • If I enter 333 and submit, the value is correctly sent to the backend.
  • However, if I enter 3333, it gets reformatted to 3,333 on the frontend. When I submit the form this value isn’t sent to the backend, so I can’t access it in the form’s clean method.

I’m not sure whether this is a JavaScript issue or something related to how Django template language that handles form inputs.

As a workaround, I currently strip the commas before submission and re-apply them on page load for display purposes.

You can clone this repository to reproduce the issue.

Any insights or suggestions?

Actually, it is. (You’re even demonstrating this by printing the form errors in your view.)

You can also see it by printing request.POST before the is_valid call, or by looking at the network tab of your browser’s developer tools after submitting the form.

However, it’s failing in the very first step of the form validation process.

If you want this value to be accepted by the form, you need to override the to_python method of the field to strip the commas.

Neither - it is a validation error as shown by the form error you’re printing.

FORM ERRORS: <ul class="errorlist"><li>cover_amount<ul class="errorlist" id="id_cover_amount_error"><li>Enter a whole number.</li></ul></li></ul>

Effectively, this is what you need to do - you can either do it in the JavaScript or you can do it in the form.