Necesito validar un campo de texto para aceptar solo números negativos o positivos y la indexación también es importante. esto es lo que he intentado hasta ahora.
este es el código HTML
<input type="text" class="negativeaccept" >aqui esta jquery
$(document).on('keypress','.negativeaccept', function(event) { if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) { event.preventDefault(); } });este código solo acepta un número positivo con un decimal. Quiero un signo negativo (-) de los índices 0.
pruebo este código y funciona (event.which != 45 || $(this).val().indexOf('-') != -1) && pero el signo negativo (-) ingresa cualquier índice. ¿Puedes probar esto mejor que esta solución?
$(document).on('keypress','.negativeaccept', function(event) { if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) || $(this).val().indexOf('.') != -1) && (event.which != 45 || $(this).val().indexOf('-') != -1) && (event.which < 48 || event.which > 57)) { event.preventDefault(); } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" class="negativeaccept">