服务器RequiredFieldValidator验证失败 设置鼠标焦点

问题:当我们使用服务器控件RequiredFieldValidator来验证文本框或者其他控件为空。提交验证失败时要求鼠标的焦点转到第一个验证失败的地方?

解决办法:

通常我们使用RequiredFieldValidator服务器控件是放在需要验证的控件后的。所以我们只要找到所有的RequiredFieldValidator并判断visibility是否是hidden,如果不是,则设置焦点在他前面的控件(只设置一次),这里我通过在每个RequiredFieldValidator加一个class:shouldbecheck,然后通过 jquery方法

$("span[id^='']").each(function() {
                if ($(this).attr("class") == "shouldbecheck" && $(this).css("visibility") != "hidden" && txt == null)
                  //找到他了

            });

Page_ClientValidate()是微软方法,是否验证成功。

control.prev()是找control的前一个元素。

代码
function CheckIsExistNull() {
if (Page_ClientValidate()) {
return true;
}
else {
alert(
"Mandatory field information missing.Please Check.");
setFoucs();
return false;
}
}

function setFoucs() {
var txt
= null;
$(
"span[id^='']").each(function() {
if ($(this).attr("class") == "shouldbecheck" && $(this).css("visibility") != "hidden" && txt == null)
txt
= $(this).prev();
});
if (txt != null) {
$(txt).focus();
}
}

 

 

 

posted @ 2010-11-15 14:05  世全  阅读(570)  评论(2)    收藏  举报