Javascript Javascript复选框表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11161956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript checkbox form validation
提问by de1337ed
I'm trying to use this link as my resource: http://www.w3schools.com/js/js_form_validation.asp
我正在尝试使用此链接作为我的资源: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.
我了解它如何用于文本框,但是如何让它检测复选框的表单验证?例如,如果我有一个带有复选框的页面(所有复选框都具有相同的“名称”值),我想确保至少选中了一个复选框......我该怎么做?如果未选中复选框,我对发送的 post 请求以及 javascript 应如何捕获它感到有些困惑。谢谢你。
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>
回答by Tim Penner
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>
回答by Jaydeep Gondaliya
function ValidateForm(form){
ErrorText= "";
if ( ( form.gender[0].checked == false ) && ( form.gender[1].checked == false ) )
{
alert ( "Please choose your Gender: Male or Female" );
return false;
}
if (ErrorText= "") { form.submit() }
}
<form name="feedback" action="#" method=post>
Your Gender: <input type="checkbox" name="gender" value="Male"> Male
<input type="checkbox" name="gender" value="Female"> Female
<input type="reset" value="Reset">
<input type="button" name="SubmitButton" value="Submit" onClick="ValidateForm(this.form)">
</form>