How to make an INPUT only accept some characters.
For example, I want to accept the numbers from 0 to 9 and only some special characters. If the entered character is not allowed, it is not marked in the input.
Thank you very much for your help
How to make an INPUT only accept some characters.
For example, I want to accept the numbers from 0 to 9 and only some special characters. If the entered character is not allowed, it is not marked in the input.
Thank you very much for your help
I would define a function in the input using Jquery
$('.input-number-line').on('input', function () {
this.value = this.value.replace(/[^0-9-]/g,'');
});
$('.input-number-guion-abajo').on('input', function () {
this.value = this.value.replace(/[^0-9_]/g,'');
});
$('.input-number').on('input', function () {
this.value = this.value.replace(/[^0-9]/g,'');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>numeros </p>
<input autofocus="" class="form-control input-number" id="coordenaday_mass" maxlength="45" required="required" type="text">
<p>numeros y linea 9-0 y ____</p>
<input autofocus="" class="form-control input-number-guion-abajo" id="coordenaday_mass" maxlength="45" required="required" type="text">
<p>numeros y linea 9-0 ----- </p>
<input autofocus="" class="form-control input-number-line" id="coordenaday_mass" maxlength="45" required="required" type="text">
I agree with Daniel, the most appropriate thing is to use the keyboard events although we will get into trouble because of the browser incomcompatibility. Just make sure when loading events to functions, read them correctly:
function presionaTecla(eventoActivado) {
var evento = eventoActivado || window.event;
switch(evento.type) {
case 'keyPress':
...........
break;
case 'KeyDown':
......
break;
}
}
Another thing, do not forget to assign events to your functions ..