Is there any method in jQuery that I can focus on input
with error? I'm trying to do validation without using the JQuery validation libs. My problem is this: the user is at the end of the screen and my validation field is in the middle. I would like to redirect the user to the field that is displaying validation message.
jQuerycode:
$(document).ready(function(){$('#region_btnSave').click(function(){vartxtHValue=$("#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);
}
});
});
And the HTML:
<span class="error" id="errortxtM" style="color:#FF0000; font-size:10px"></span>
He is doing the validation right but if the user has at the end of the screen, he will not be able to see the message "Required".