I want to validate that when pressing a button it is validated if the input, in case it is empty, an alert appears and that it focuses on that input, but what happens is that if it is empty, the alert comes out but sends me to another page.
Form:
<html>
<head>
<title>prueba</title>
</head>
<body>
<form onsubmit="control()" action="https://stackoverflow.com">
<input type="text" id="uname" placeholder="Ingrese usuario..." name="uname">
<input type="password" placeholder="Ingrese contraseña..." name="psw">
<button type="submit">Iniciar sesión</button>
</form>
<script>
function control() {
if (document.getElementById('uname') == "") {
alert("El campo no puede estar vacío.");
document.getElementById('uname').focus();
return false;
}
return true;
}
</script>
</body>
</html>
The script doesn't work 'cause when i press the button and input text is empty this no shows the alert and redirect me to page. Any sugerence?