需要帮助检查验证的单选按钮JavaScript

Need help checking verification of radio buttons JavaScript

本文关键字:单选按钮 JavaScript 验证 检查 帮助      更新时间:2023-09-26

我正在做GCSE计算课程作业任务,我试图在一组3个单选按钮上获得验证,但是单选按钮的代码(我从外部源复制并粘贴,我现在不记得了),但是这个复制和粘贴的代码似乎覆盖了其他字段的验证(两个文本输入字段和一个数字输入字段)代码如下所示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1    /DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exam Entry</title>
<script language="javascript"" type="text/javascript">
function validateForm() {
            var result = true;
            var msg="";
if (document.ExamEntry.name.value=="") {
            msg+="You must enter your name 'n";
            document.ExamEntry.name.focus();
            document.getElementById('name').style.color="red";
            result = false;
}
if (document.ExamEntry.subject.value=="") {
            msg+="You must enter the subject 'n";
            document.ExamEntry.subject.focus();
            document.getElementById('subject').style.color="red";
            result = false;
}
if (document.ExamEntry.examnumber.value=="") {
            msg+="You must enter the exam number 'n";
            document.ExamEntry.examnumber.focus();
            document.getElementById('examnumber').style.color="red";
            result = false;
}
if (document.ExamEntry.examnumber.value.length!=4) {
 msg+="Your exam number must be exactly 4 digits 'n";
            document.ExamEntry.examnumber.focus();
            document.getElementById('examnumber').style.color="red";
            result = false;
}
var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked) {
       checked = inputs[i];
       break;
}
}
if(checked==null)
{
alert('Please choose an option');
return false;
}
else{
return confirm('You have chosen '+checked.value+' is this correct?');
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
            }
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
  <table width="50%" border="0">
            <tr>
                            <td id="name">Name</td>
                            <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                            <td id="subject">Subject</td>
                            <td><input type="text" name="subject" /></td>
            </tr>
            <tr>
                            <td id="examnumber">Exam Number</td>
                            <td><input type="number" name="examnumber" size="4" maxlength="4"></td>
            </tr>
            <tr>
                            <td><input type="radio" id="examtype" name="examtype" value="GCSE" /> : GCSE<br/>
                            <input type="radio" id="examtype" name="examtype" value="A2" /> : A2<br/>
                            <input type="radio"     id="examtype" name="examtype" value="AS"/> : AS<br/>
            </tr>
            <tr>
                            <td><input type="submit" name="Submit" value="Submit"     onclick="return validateForm();" /></td>
                            <td><input type="reset" name="Reset" value="Reset" /></td>
            </tr>
</table>
</form>
</body>
</body>
</html>

HTML文档中仍然有一些错误:

  • 复制ID的
  • 未封闭<input>标签
  • javascript中缺少else语句
  • 复制</body>标签
  • <script>标签中额外的双引号

我修复了这些。我还将msg变量的警报移到了选项检查的上方,这样它们就不会相互干扰。为了使第一个字段而不是最后一个字段成为焦点,我添加了一个检查来查看result是否仍然为真:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exam Entry</title>
    <script language="javascript" type="text/javascript">
        function validateForm() {
            var result = true;
            var msg="";
            if (document.ExamEntry.name.value=="") {
                msg+="You must enter your name 'n";
                if(result) document.ExamEntry.name.focus();
                document.getElementById('name').style.color="red";
                result = false;
            }
            if (document.ExamEntry.subject.value=="") {
                msg+="You must enter the subject 'n";
                if(result) document.ExamEntry.subject.focus();
                document.getElementById('subject').style.color="red";
                result = false;
            }
            if (document.ExamEntry.examnumber.value=="") {
                msg+="You must enter the exam number 'n";
                if(result) document.ExamEntry.examnumber.focus();
                document.getElementById('examnumber').style.color="red";
                result = false;
            }
            if (document.ExamEntry.examnumber.value.length!=4) {
                msg+="Your exam number must be exactly 4 digits 'n";
                if(result) document.ExamEntry.examnumber.focus();
                document.getElementById('examnumber').style.color="red";
                result = false;
            }
            if(msg != ""){
                alert(msg);
                return result;
            }
            var checked = null;
            var inputs = document.getElementsByName('examtype');
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].checked) {
                    checked = inputs[i];
                    break;
                }
            }
            if(checked==null) {
                alert('Please choose an option');
                return false;
            }
            else {
                return confirm('You have chosen '+checked.value+' is this correct?');
            }
        }
    </script>
</head>
    <body>
        <h1>Exam Entry Form</h1>
        <form name="ExamEntry" method="post" action="success.html">
          <table width="50%" border="0">
                    <tr>
                                    <td id="name">Name</td>
                                    <td><input type="text" name="name" /></td>
                    </tr>
                    <tr>
                                    <td id="subject">Subject</td>
                                    <td><input type="text" name="subject" /></td>
                    </tr>
                    <tr>
                                    <td id="examnumber">Exam Number</td>
                                    <td><input type="text" name="examnumber" size="4" maxlength="4"/></td>
                    </tr>
                    <tr>
                                    <td><input type="radio" id="examtypeGCSE" name="examtype" value="GCSE" /> : GCSE<br/>
                                    <input type="radio" id="examtypeA2" name="examtype" value="A2" /> : A2<br/>
                                    <input type="radio" id="examtypeAS" name="examtype" value="AS"/> : AS<br/>
                    </tr>
                    <tr>
                                    <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
                                    <td><input type="reset" name="Reset" value="Reset" /></td>
                    </tr>
            </table>
        </form>
    </body>
</html>