[Django]-Loading decimal numbers from django form (formset) into number input

1đź‘Ť

âś…

Ok, I’ve done something like this. First of all I am using an input type=”text”. Then using jQuery.numeric() I convert my input so that it only accepts numbers, and a , as decimal separator.

$("#field_id").numeric({ decimal : "," });

Now every time i submit my form I have some code that converts my , into . so that it fits the backend logic:

$('#recipe-form').on('submit', function(){
    $('.convert-separator').each(function () {
        $(this).val($(this).val().replace(',','.'));
    });
    this.submit();
});

But I think it’s not the best solution. I think there must be a possibility to use the input type="number" field with a , separator.

👤losik123

1đź‘Ť

You could set your locale decimal separator

UPDATE:

Didnt saw it is jquery related,

you could create new validator class for comma numbers for instance

jQuery.validator.addMethod("commanumber", function (value, element) {
    return this.optional(element) || /^(\d+|\d+,\d{1,2})$/.test(value);
}, "Please specify the correct number format");

or even extend regex to support . also

👤iklinac

0đź‘Ť

You can convert the count value in your jQuery code:

dotCount = count.toString().replace(',', '.');

This replaces the comma with a dot

👤dentemm

0đź‘Ť

I found nice article which one can describe your problem. Author recommended to use lang attribute e.g <html lang="en-150"> or <input type="number" lang="en-150">.

There is also a note on www.w3.org

4.10.5.2 Implemention notes regarding localization of form controls

This section is non-normative.

The formats shown to the user in date, time, and number controls is
independent of the format used for form submission.

Browsers are encouraged to use user interfaces that present dates,
times, and numbers according to the conventions of either the locale
implied by the input element’s language or the user’s preferred
locale. Using the page’s locale will ensure consistency with
page-provided data.

For example, it would be confusing to users if an American English
page claimed that a Cirque De Soleil show was going to be showing on
02/03, but their browser, configured to use the British English
locale, only showed the date 03/02 in the ticket purchase date picker.
Using the page’s locale would at least ensure that the date was
presented in the same format everywhere. (There’s still a risk that
the user would end up arriving a month late, of course, but there’s
only so much that can be done about such cultural differences…)

👤M. Galczynski

Leave a comment