Loading decimal numbers from django form (formet) into numeric input
I am creating a simple django app where you can create recipes with ingredients. In my view RecipeCreate
, I have a simple set of forms with an input select
(where you select a product) and input type="number"
indicating the number of products. This way my form and model are pretty simple:
class Ingredient(models.Model):
product = models.ForeignKey(Product)
count = models.DecimalField(max_digits=4, decimal_places=2)
class IngredientForm(forms.ModelForm):
class Meta:
model = Ingredient
fields = ('product', 'count')
Now when I want to display my recipe in my view RecipeEdit
everything loads fine except for the input count
. I am getting the following jQuery validation error:
The specified value "12,00" is not a valid number. The value
must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?
Since I am writing a Polish site, I would like our national standard decimal separator to be ,
not a .
(but the period must also be the separator).
One solution would be to use input type="text"
, but that would mean that I have to check my number manually, which is awkward. Is there a way to pass my number 12,00
so that it is accepted in the input field. Perhaps changing / overwriting the standard jQuery validation? But I will agree with any good solution suggested.
Another curious fact: when I write by hand 12,00
or 12.00
and submit my form, my form is valid and the count field Ingredient
is od 12 which I desire. This is another pro for input type="number"
.
source to share
You can set your locale decimal separator
UPDATE:
Didn't see it related to jQuery,
you could create a new validation class for comma numbers like
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 for support. also
source to share
Ok I did something like this. First of all, I am using input type = "text". Then, using jQuery.numeric (), I convert my input so that it only accepts numbers and ,
as a decimal separator.
$("#field_id").numeric({ decimal : "," });
Now, every time I submit my form, I have some code that converts mine ,
to .
so that it matches the backend logic:
$('#recipe-form').on('submit', function(){
$('.convert-separator').each(function () {
$(this).val($(this).val().replace(',','.'));
});
this.submit();
});
But I think this is not the best solution. I think it should be possible to use a input type="number"
delimited field ,
.
source to share
I found a nice article to describe your problem. The author recommended using the lang attribute like <html lang="en-150">
or <input type="number" lang="en-150">
.
There is also a note at www.w3.org
4.10.5.2 Implementation notes regarding localization of form controls
This section is not normative.
The formats that are displayed to the user in the date, time, and number parameters are independent of the format used to submit the form.
The browser is encouraged to use user interfaces that represent dates, times, and numbers according to conventions, either the locale implied by the language of the input elements or the user's preferred locale. Using the page locale will ensure consistency with the provided pages.
For example, it would confuse users if American English stated that the Cirque De Soleil show would air on 02/03, but their browser, configured to use the British English locale, showed 03/02 in the ticketing date pick. Using the locale of the page would at least ensure that the date was represented in the same format throughout. (There is still a risk that of course the user will end up arriving a month late, but there is only so much that can be done about such cultural differences ...)
source to share