I have an input and if a number greater than 100 is written in that input, then I get an alert() telling me the number cannot exceed 100, I want to achieve this without the need for a Submit . Let it be in real time
He validates that my input allows only a number, but I still can't achieve my stated problem
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="Content/Site.css" rel="stylesheet" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="text" id="cobertura" name="cobertura" onkeypress="return checka(event)" maxlength="3" />
</form>
<script>
function checka(e) {
// SOLO ACEPTA NUMERO
tecla = (document.all) ? e.keyCode : e.which;
if (tecla == 8) {
return true;
}
patron = /[0-9]+$/;
tecla_final = String.fromCharCode(tecla);
return patron.test(tecla_final);
// END
//ESTA PARTE ES LA QUE QUIERO VALIDAR
var num= document.getElementById('cobertura').value;
if (num> 100) {
alert('Numero no puede pasar de 100');
return false;
}
// END
}
</script>
</body>
</html>
Hello my friend how are you.
What I would do in your case would be to put an event that listens every time I write to the input so that in this way I can constantly validate.
I do it in a simple way but now you can add all the difficulty, styles and others you want, or if it is the case a framework.
I hope you find it useful, the code seems to me to be well explained but I still leave you some references used to guide you:
Events: https://developer.mozilla.org/es/docs/Web/API/EventTarget/addEventListener
Event type "input": https://developer.mozilla.org/es/docs/Web/API/HTMLElement/input_event
alert()
: https://developer.mozilla.org/en/docs/Web/JavaScrihttps://developer.mozilla.org/en/docs/Web/API/Window/alertpt/Reference/Global_Objects/parseIntI hope it helps you, bye <3
For those who want to validate by id and not by input