I've been trying to use JavaScript to validate only numbers in a field. But isn't working
Here I have the JavaScript code:
<script language="JavaScript">
function onlyNumbers(evt)
{
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
and here I have the WTForms that I am using with Flask:
{{ form.saturacion.label(class="form-control-label") }}
{% if form.saturacion.errors %}
{{ form.saturacion(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.saturacion.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.saturacion(class="form-control form-control-lg") }}
{% endif %}
These two are in the html file
And here I have the form:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, SelectField,RadioField,IntegerField
from wtforms.validators import DataRequired,Length, Email, EqualTo,ValidationError,InputRequired
from init.models import User
class PredictionForm(FlaskForm):
saturacion = IntegerField('Saturacion de O2', validators=[InputRequired(), Length(min=1, max=2)])
Could someone please tell me how to allow only numbers in that field with WTForms?