Javascript复选框表单验证 [英] Javascript checkbox form validation

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

问题描述

我尝试将此链接用作我的资源: http://www.w3schools。 com / js / js_form_validation.asp



我理解它如何适用于文本框,但是如何让它检测复选框的表单验证?例如,如果我有一个排列着复选框的页面(所有页面都具有相同的名称值),我想确保至少有一个框被选中...我该怎么做?如果复选框未被选中,以及javascript应该如何捕获,我对发送post请求有点困惑。感谢您。

编辑----



得到它的工作,以下是未来人员的一些代码:

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascript>
函数Validate(){
if(!validateForm()){
alert(Something happened);
返回false;

返回true

函数validateForm()
{
var c = document.getElementsByTagName('input');
for(var i = 0; i< c.length; i ++){
if(c [i] .type =='checkbox')
{
if(c [i] .checked){return true}
}
}
return false;
}
< / script>
< / head>
< body>
< form name =myFormaction =demo_form.asponsubmit =return Validate()method =post>
值:< input type =checkboxname =fnamevalue =value>
Value2:< input type =checkboxname =fnamevalue =value2>
...<这里有更多箱子>
< input type =submitvalue =提交>
< / form>
< / body>
< / html>


解决方案

下面是一个例子,可以在此页面中进行测试:

复选框);返回false; } return true} function validateForm(){var c = document.getElementsByTagName('input'); for(var i = 0; i< c.length; i ++){if(c [i] .type =='checkbox'){if(c [i] .checked){return true}}} return false;}

< form name =myFormaction =demo_form .asponsubmit =return Validate()method =post>选项1:< input type =checkboxname =option1value =1>< br / < input type =checkboxname =option2value =2>< br />< input type =submitvalue =Submit Form>< / form>


I'm trying to use this link as my resource: http://www.w3schools.com/js/js_form_validation.asp

I understand how it works for textboxes, but how do you make it detect form validation for checkboxes? For example, if I have a page lined with checkboxes (all with the same "name" value), I want to make sure that at least one of the boxes is checked... How do I do this? I'm a little confused about what post request is sent if a checkbox is not checked, and how javascript should catch it. Thank you.

Edit----

Got it to work, here's some code for future people:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function Validate(){
   if(!validateForm()){
       alert("Something happened");
       return false;
   }
return true
}
function validateForm()
{
    var c=document.getElementsByTagName('input');
    for (var i = 0; i<c.length; i++){
        if (c[i].type=='checkbox')
        {
            if (c[i].checked){return true}
        }
    }
    return false;
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return Validate()" method="post">
Value: <input type="checkbox" name="fname" value="value">
Value2: <input type="checkbox" name="fname" value="value2">
...<more boxes here>
<input type="submit" value="Submit">
</form>
</body>
</html>

解决方案

Here is an example that does what you are asking and can be tested within this page:

function Validate(){
   if(!validateForm()){
       alert("You must check atleast one of the checkboxes");
       return false;
   }
return true
}
function validateForm()
{
    var c=document.getElementsByTagName('input');
    for (var i = 0; i<c.length; i++){
        if (c[i].type=='checkbox')
        {
            if (c[i].checked){return true}
        }
    }
    return false;
}

<form name="myForm" action="demo_form.asp" onsubmit="return Validate()" method="post">
Option 1: <input type="checkbox" name="option1" value="1"><br />
Option 2: <input type="checkbox" name="option2" value="2"><br />
<input type="submit" value="Submit Form">
</form>

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

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