MS RequiredFieldValidator 不支援 CheckBoxList ,
只能用 CustomValidator Call Client Script 去驗證
參考了一些網站,所以衍生出以下寫法,
因jQuery的寫法比較沒有跨瀏覽器問題,所以比較建議~
不過目前寫法應該只能這樣吧...搞不好以後MS自己也會支援...不需要另外CODING

~Shael

<asp:CheckBoxList ID="chkboxlist1" runat="server">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBoxList ID="chkboxlist2" runat="server">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="cv_chkboxlist1" runat="server" ClientValidationFunction="ValidateChkBoxList" ErrorMessage="chkboxlist1"></asp:CustomValidator>
<asp:CustomValidator ID="cv_chkboxlist2" runat="server" ClientValidationFunction="ValidateChkBoxList" ErrorMessage="chkboxlist2"></asp:CustomValidator>

 

function ValidateChkBoxList(source, args) {
var items = $("input[name*='" + source.id.replace("cv_", "") + "']");
for (i = 0; i < items.length; i++) {
if ($(items[i]).is(":checked")) {
args.IsValid = true;
return;
}
}
args.IsValid = false;
}

 參考網址如下:

Passing value from CustomValidator to ClientValidationFunction
http://forums.asp.net/t/1296600.aspx?Passing+value+from+CustomValidator+to+ClientValidationFunction

CustomValidator.ClientValidationFunction Property
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx

ASP.NET CheckBoxList Client side validation
http://stackoverflow.com/questions/11979472/asp-net-checkboxlist-client-side-validation

使用CustomValidator搭配jQuery進行CheckBoxList的Required驗證(單一個)
http://www.dotblogs.com.tw/terrychuang/archive/2011/01/25/21002.aspx

Required Field Validator for CheckBoxList
http://himanshu214.blogspot.tw/2013/07/required-field-validator-for.html

CheckBoxList Validation in ASP.Net - Required Field Validator
http://www.codeproject.com/Articles/28560/CheckBoxList-Validation-in-ASP-Net-Required-Field

 

arrow
arrow
    文章標籤
    .Net jQuery CustomValidator
    全站熱搜

    shael 發表在 痞客邦 留言(0) 人氣()