如何使用javascript验证验证asp.net控件中的复选框列表 [英] how to validate checkbox list in asp.net controls using javascript validation
本文介绍了如何使用javascript验证验证asp.net控件中的复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何使用javascript validation验证asp.net控件中的复选框。如果未选中该复选框,则应显示错误消息。
please tell how to validate the checkbox in asp.net control using javascript validation.if the checkbox is not selected it should display an error message.
推荐答案
您好,
这是使用customvalidator的解决方案,不是完全javascript解决方案,但也许可以使用:
http://www.4guysfromrolla.com/webtech/tips/t040302-1.shtml [ ^ ]
或
与自定义验证器和一小段javscript一起代码如下:
Hi,
Here's is a solution using customvalidator, not entirely javascript solution, but maybe it could be used:
http://www.4guysfromrolla.com/webtech/tips/t040302-1.shtml[^]
Or
Together with a custom validator and a small piece of javscript as code below:
function validate(source, arguments) {
arguments.IsValid = false;
var checklist = document.getElementById("MyCheckBoxList");
if (checklist == null) return;
var elements = checklist.getElementsByTagName("INPUT");
if (elements == null) return;
var checkBoxCount = 0;
for (i = 0; i < elements.length; i++) {
if (elements[i].checked) checkBoxCount++;
}
arguments.IsValid = (checkBoxCount > 0);
}
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="validate" ErrorMessage="One of the checkboxes needs to be checked!"
ValidationGroup=""></asp:CustomValidator>
问候
Joachim
Regards
Joachim
Write the bellow code with in validation funtion in javascript
var chkControl= document.getElementById("<%chk.ClientID%>");
if(!chkControl.checked )
{
alert("Required to Check!!")
}
将以下Javascript放入函数函数ValidateForm(form)
Put the following Javascript in your function function ValidateForm(form)
if ( form.terms.checked == false )
{
alert ( "Please check the Conditions box." );
return false;
}
这篇关于如何使用javascript验证验证asp.net控件中的复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文