要驗證 CheckBox 是否勾選,使用CustomValidator驗證控制項
參考MSDN資料:
http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction%28v=VS.100%29.aspx
ClientValidationFunction
MSDN說明 : 取得或設定驗證使用的自訂用戶端指令碼函式名稱。
============= HTML ================================
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="請勾選..." ClientValidationFunction="ClientValidate" ForeColor="Red" ></asp:CustomValidator>
function ClientValidate(source, arguments) {
var chk = document.getElementById("CheckBox1").checked;
arguments.IsValid = chk;
}
============= Javascript Function=========================
參考MSDN的範例改的,沒勾CustomValidator的ErrorMessage會秀出來
function ClientValidate(source, arguments) {
var chk = document.getElementById("CheckBox1").checked;
arguments.IsValid = chk;
}
DropDownList 要這樣寫
function ValidateGenderSelection(source, arguments)
{
var genderList = document.getElementById("GenderDropDownList");
if (null != genderList)
{
var iValue = new Number(genderList[genderList.selectedIndex].value);
arguments.IsValid=(iValue > 0);
}
else
{
arguments.IsValid = false;
}
}
// ASP.NET CODE 別設定 ControlToValidate
<asp:DropDownList ID="GenderDropDownList" runat="server"> />
<asp:CustomValidator ID="GenderSelectionRequiredValidator" runat=server
ClientValidationFunction="ValidateGenderSelection"
ErrorMessage="Select Gender" ForeColor="Red"
Display=Dynamic>
</asp:CustomValidator>
沒有留言:
張貼留言