Hola tinito 22 y bienvenido a los foros.
Todo lo que tienes que hacer es preguntar por el control antes de validarlo. Fíjate en este ejemplo:
Código PHP:
<html>
<head>
<script language="JavaScript">
function val(){
if (document.frm.uno)
if (document.frm.uno.value == ""){
alert("Invalido");
document.frm.uno.focus();
return false;
}
if (document.frm.dos)
if (document.frm.dos.value == ""){
alert("Invalido");
document.frm.dos.focus();
return false;
}
if (document.frm.tres)
if (document.frm.tres.value == ""){
alert("Invalido");
document.frm.tres.focus();
return false;
}
alert("Ok");
return true;
}
</script>
</head>
<body>
<form name="frm">
<input type="text" name="uno"><br>
<input type="text" name="tres"><br>
<input type="button" name="btn" value="validar" onclick="val()">
</form>
</body>
</html>
Tengo los siguientes elementos válidos:
uno y
tres. El elemento
dos no es válido y sin embargo la función no falla.
Saludos.