0

Tengo esta expresion regular que valida hasta 10 enteros un punto decimal y hasta 10 decimales,¿Como puedo implementar esta expresion regular en un textbox para que si no se cumple la validacion me marque un error o me detenga la escritura

<script>
$(document).ready(function () {

$('#decimalEntero').on('input', function (e) {
    if (/^(\d{1,10})(.\d{1,10})?$/i.test(this.value)) {
        alert("Ok"); 
    }else{
        alert("error");
    }
});
});
</script>
<html>
<head>
<title>10 Enteros + 10 decimales</title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form action="#">
<p>
<input type="text" name="valor" id="decimalEntero" /><br />
</form>
</body>
</html>

1 respuesta 1

0

Podrías utilizar HTML5 y los eventos oninvalid y oninput y sin necesidad de utilizar JavaScript.

OJO: Si quieres utilizar esto en un formulario que ingresa datos en una base de datos, no olvides validar también utilizando un script del lado del servidor (server-side scripting).

<form action="#">
<p>
<input type="text" name="valor" id="decimalEntero" pattern="(\d{1,10})(.\d{1,10})?" oninvalid="setCustomValidity('No es válido')" oninput="setCustomValidity('')" /><br />
</form>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.