[英]How to validate specific aspects of a web form?
Being fairly new to JavaScript, I am struggling with how to target and validate specific areas of a form. 对于JavaScript来说,它还很陌生,我在努力寻找目标并验证表单的特定区域。
I have created a simple web form, which consists of two text input fields for first name and last name. 我创建了一个简单的Web表单,该表单包含两个文本输入字段,分别用于名字和姓氏。 A checkbox with four options, and a submit button.
具有四个选项和一个提交按钮的复选框。 With the form created, I wish to ensure the user:
创建表格后,我希望确保用户:
I have attempted number 2 and have so far got the following: 我尝试过2号,到目前为止得到了以下信息:
function checkBox (){
var cbx = document.getElementById("myForm").cbx;
var chbx = false;
for (i=0; <cbx.length; i++) {
if(cbx[i].checked){
chbx = true;
// if not produce an alert
if (!chbx) {
alert("No checkboxes selected");
}
If the above is functionally correct, it only works when no checkboxes are selected, what if too many are selected? 如果以上内容在功能上都是正确的,则仅在未选中任何复选框的情况下才起作用,如果选中太多复选框该怎么办? How do i go about these three problems?
我该如何解决这三个问题?
First things first, you have some syntax errors. 首先,您有一些语法错误。 Please try the following code and see if it's working any better.
请尝试以下代码,看看效果是否更好。
function checkBox (){
var cbx = document.getElementById("myForm").cbx;
var chbx = false;
for (i=0; <cbx.length; i++) {
if(cbx[i].checked){
chbx = true;
// if not produce an alert
}
if (!chbx) {
alert("No checkboxes selected");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.