0
function validar() {
var nome = document.getElementById("nome_passageiro").value;
var data = document.getElementById("dt_nascimento").value;
var cpf = document.getElementById("cpf").value;
var modelo = document.getElementById("modelo").value;
var status = document.getElementById("status").find("option[value='"+val+"']");
if (nome == "") {
alert('Preencha o campo com seu nome');
}else if(data==""){
alert('Preencha a data');
}else if(cpf==""){
alert('Preencha o cpf');
}else if(modelo==""){
alert('Preencha o modelo');
}else if(status==""){
alert('Preencha o Status');
}else{
return false;
}
}
<h1>Cadastro de Motoristas</h1>
Nome do Motorista:
<input type="text" id="nome_passageiro" /><br/><br/>
Data de Nascimento:
<input type="text" id="dt_nascimento" /><br/><br/>
CPF:
<input type="text" id="cpf" onkeypress="" /><br/><br/>
Modelo de Carro:
<input type="text" id="modelo" /><br/><br/>
Status:
<input list="status" /><br/><br/>
<datalist id="status">
<option value="-------">
<option value="Ativo">
<option value="Inativo">
</datalist>
<button type="button" onkeypress="validar()">Cadastrar</button>
I’m doing a field validation using Javascript in a jsp application but it’s not working.
guy switched to onclick and didn’t work
– User1999
Strange, while running the code here worked. What error appears?
– Caique Romero
Ahh appeared another rs error, in the status condition, in the case status is a data list with two active and inactive options, it occurs the error in the following line. var status = Document.getElementById("status"). find("option[value='"+val+"']"); Uncaught Referenceerror: val is not defined
– User1999
That’s because your variable val was not defined.
– Caique Romero
this in case I am wanting to get what the user will select in the data list
– User1999
it worked dude I fixed the val and it worked vlw
– User1999
You could not use select instead of this datalist?
– Caique Romero