limit numbers and commas x jquery

0

I have an input text, where I would like to limit the characters entered by the user to "only numbers and commas", that is:

Ej1: "1001,1002,1003" Ej2: "1001" Ej3: "1001,2002"

How could I do it in jquery? I copy fragment of the code:

			<div id="marco">
				<span id="nombre_campo">Ingrese el Nº de planilla : </span><input type="text" data-planilla="" id="cajita" name="ingreso" autofocus>
				<input type="button" id="boton1" name="enviar" data-planilla="NO"  onclick="Muestro_Valor()" value="Buscar">
			</div>
    
asked by look68 13.12.2018 в 19:25
source

1 answer

1

With that script you only let the input put number and commas.

	$('.input-number').on('input', function () { 
this.value = this.value.replace(/[^0-9,]/g,'');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input-number" value="" />

Responding to your question in the comment I leave an example of how it occurred to me to do it is likely to have 10,000 better forms but I just came out that. Probe doing it only with addClass and removeClass but it does not work it must be for the initialization of the input so I directly remove it from the sun and I generate it again with another class. I repeat, take it as a "guide" of what the idea may be, I just did it, I look at it and I tell you for sure that there must be a much better way.

 $('#select').on('change', function() {

 	if(this.value == 0){
 		$("#cajita").remove()
 		$("#contenedor").html("<input type='text' data-planilla=' id='cajita' name='ingreso' autofocus class='conRestriccion' placeholder='Aplica restriccion'>")
 	}
 	else{
 		$("#cajita").remove()
 		$("#contenedor").html("<input type='text' data-planilla=' id='cajita' name='ingreso' autofocus class='sinRestriccion' placeholder='Aplica sin restriccion'>")
 	}

$('.conRestriccion').on('input', function () { 
	this.value = this.value.replace(/[^0-9,]/g,'');
});

});
<script
  src="https://code.jquery.com/jquery-1.9.0.js"
  integrity="sha256-TXsBwvYEO87oOjPQ9ifcb7wn3IrrW91dhj6EMEtRLvM="
  crossorigin="anonymous"></script>

<select name="" id="select">
<option value="vacio">Seleccionar una opcion</option>
<option value="0">Con restriccion</option>
<option value="1">Sin restriccion</option>
</select>

<br><br>
<div id="marco">
<span id="nombre_campo">Ingrese el Nº de planilla : </span>
<div id="contenedor">
	<input type="text" data-planilla="" id="cajita" name="ingreso" autofocus class="sinRestriccion" placeholder="Inicia sin restricción">
</div><br>
<input type="button" id="boton1" name="enviar" data-planilla="NO"  onclick="Muestro_Valor()" value="Buscar">
</div>
    
answered by 13.12.2018 / 19:31
source