Here is another solution:
- Add validators to the model file
- Add css class to fields in model form
- Use jquery to change the type of the specified class to "number" on document / page load
IN MODEL:
exp_from = models.IntegerField(default=0, validators=[ MaxValueValidator(100), MinValueValidator(0) ]) exp_to = models.IntegerField(default=1, validators=[ MaxValueValidator(100), MinValueValidator(1) ])
IN MODEL FORM - add a jquery class
self.fields['exp_from'].widget.attrs['class'] = "text_type_number" self.fields['exp_to'].widget.attrs['class'] = "text_type_number"
IN FILE / JS FILE
$(document).ready(function(){ $('.text_type_number').attr('type', 'number'); });
source share