Existe algum método no jQuery que eu consiga focar no input
com erro? Estou tentando fazer validação sem usar as libs de validação do JQuery. Meu problema é o seguinte: o usuário está no fim da tela e meu campo de validação está no meio. Queria redirecionar o usuário para o campo que está apresentando mensagem de validação.
Código do jQuery :
$(document).ready(function()
{
$('#region_btnSave').click(function ()
{
var txtHValue = $("#region_tabVersao_N_txtH").val();
var txtMValue = $("#region_tabVersao_N_txtM").val();
if (txtMValue == '' && txtHValue == '') {
$("#errortxtM").text("Obrigatório").focus();
$("#errortxtH").text("Obrigatório");
$("#region_tabVersao_N_txtM").keydown(function () {
$("#errortxtM").text("").focus()
});
$("#region_tabVersao_N_txtH").keydown(function () {
$("#errortxtH").text("")
});
return false;
} else if (txtMValue == '') {
$("#errortxtM").text("Obrigatório");
$("#region_tabVersao_N_txtM").keydown(function () {
$("#errortxtM").text("")
});
return false;
} else if (txtHValue == '') {
$("#errortxtH").text("Obrigatório");
$("#region_tabVersao_N_txtH").keydown(function () {
$("#errortxtH").text("")
});
return false;
}
else {
//not all text fields are valid
$("#region_tabVersao_N_txtH").after('', null);
$("#region_tabVersao_N_txtM").after('', null);
}
});
});
E o HTML:
<span class="error" id="errortxtM" style="color:#FF0000; font-size:10px"></span>
Ele ta fazendo a validação certinho porem se o usuário tiver no fim da tela ,ele não vai conseguir ver a mensagem de "Obrigatório".
$("#errortxtM").focus();
ao invés de$("#errortxtM").text("Obrigatório").focus();
???errortxtM
parece ser oid
do texto abaixo doinput
, qual é oid
doinput
? Porque é nele que você tem que dar ofocus
.