复选框验证 [英] checkbox validation

查看:105
本文介绍了复选框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何验证复选框控件.....我已经对文本框进行了验证...是否有要使用的dll文件....

解决方案

您可以使用javascript
简单地做到这一点
我假设您的表单上有多个复选框,并且您想检查是否在单击按钮时至少选中了一个复选框

在您的源代码中添加以下代码.只需记住"myform"是页面形式的ID

<input type="checkbox" name="check_list" value="1" />check 1<br />
        <input type="checkbox" name="check_list" value="2" />check 2<br />
        <input type="checkbox" name="check_list" value="3" />check 3<br />
        <input type="checkbox" name="check_list" value="4" />check 4<br />
        <input type="checkbox" name="check_list" value="5" />check 5<br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ValidateChecks(document.myform.check_list)"/>



并添加以下javascript函数

function ValidateChecks(chk)
    {
        var flag=0;
        for (i = 0; i < chk.length; i++)
        {
            if(chk[i].checked==true)
            {
                flag=1;
            }
        }
        if(flag==0)
        {
            alert("You Did not select any checkbox.");
        }
     }



希望对您有所帮助


您要使用哪种类型的验证?

复选框有两个选项:true/false.

您不能将其设置为必需,因为false是复选框的选项. :(


您只是拒绝买书,不是吗?

您可以编写一个自定义验证器,该验证器可遍历您的复选框并强制执行您的条件(我假设您希望至少选择一个).还应在服务器上检查所有验证,最坏的情况是某些事情最终需要发回验证.

您还在为那些认为您知道如何编写代码的人,或者更确切地说,您的老板雇用了真正的程序员的人正在编写的身份证网站?您是否知道任何人都不遵循任何编码标准,对您的代码进行编码会有多困难?还是让任何真正的开发人员在您的表示层中看到您的数据库代码时都感到震惊吗?还是您对基本数据库安全性缺乏了解?认真地说,您是否想成为一名程序员,还是只是干脆完成这份工作,直到您的老板(我们确定的老板是白痴)决定他可以做得更好并解雇您?提出您要问的那种问题,也许会使您的代码可以拼凑在一起,但这不会教您编程的基础知识.

如果您真的想成为一名程序员,则需要辞掉工作的这种玩笑,告诉客户您的老板是骗子和小偷,然后上某种课程.


how to validate a checkbox controls.....I have made validation for textbox ... is there any dll files to be used....

解决方案

You can simply do this with javascript

I assume you have several checkboxes on your form and you want to check whether atleast one checkbox is checked or not on button click

add the following code in your source. just remember ''myform'' is the id of the form of your page

<input type="checkbox" name="check_list" value="1" />check 1<br />
        <input type="checkbox" name="check_list" value="2" />check 2<br />
        <input type="checkbox" name="check_list" value="3" />check 3<br />
        <input type="checkbox" name="check_list" value="4" />check 4<br />
        <input type="checkbox" name="check_list" value="5" />check 5<br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ValidateChecks(document.myform.check_list)"/>



and add the following function of javascript

function ValidateChecks(chk)
    {
        var flag=0;
        for (i = 0; i < chk.length; i++)
        {
            if(chk[i].checked==true)
            {
                flag=1;
            }
        }
        if(flag==0)
        {
            alert("You Did not select any checkbox.");
        }
     }



I hope you find this helpful


What type of validation you want to have?

Checkbox has two option : true / false.

You cant make it required as false is an option for checkbox. :(


You just refuse to buy a book, don''t you ?

You could write a custom validator that iterates over your checkboxes and enforces your condition ( I assume you want at least one selected ). ALL validation should also be checked on the server, worst case is that some things end up requiring post back to validate.

Is this still for the id card site that you''re writing for someone who thinks you know how to write code, or rather, that your boss hires real programmers ? Do you have any idea how hard it will be for anyone to ever work on your code, because you don''t follow any sort of coding standard ? Or how shocked any real developer would be to see your database code in your presentation layer ? Or your lack of understanding of basic database security ? Seriously, do you want to become a programmer, or just ride out this job until even your boss ( who we have established is a moron ) decides that he can do better and fires you ? Asking the sort of questions you''re asking, will perhaps get you code you can cobble together, but it won''t teach you the basics of programming.

You need to quit this joke of a job, tell the client that your boss is a liar and a thief, and get into some sort of course, if you really want to be a programmer.


这篇关于复选框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆