1
Is there any method in jQuery that I can focus on input
with error? I am trying to validate without using the validation libs from Jquery. My problem is this: the user is at the bottom of the screen and my validation field is in the middle. I wanted to redirect the user to the field that is displaying validation message.
Code of the 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);
}
});
});
And the HTML:
<span class="error" id="errortxtM" style="color:#FF0000; font-size:10px"></span>
He is doing the validation certificate however if the user has at the end of the screen ,he will not be able to see the message "Required".
Shouldn’t be
$("#errortxtM").focus();
instead of$("#errortxtM").text("Obrigatório").focus();
???– Jéf Bueno
https://stackoverflow.com/questions/14719269/assign-scroll-animation-to-focus-in-jquery .
– alxwca
errortxtM
seems to be theid
of the text below theinput
, what is theid
ofinput
? Why is it that you have to give thefocus
.– Roberto de Campos