我正在尝试使用JavaScript字符串来确保examnumber字段中输入的数据正好是4位数字,有人能帮忙吗?

我正在尝试使用JavaScript字符串来确保examnumber字段中输入的数据正好是4位数字,有人能帮忙吗?,javascript,html,Javascript,Html,这是我到目前为止所做的,我想设置,以便输入到考试编号字段的文本正好是4位数字。我遇到了麻烦,因为无论我尝试什么都不起作用,如果有人能帮我一把,甚至在那之后发布完整的代码,那就太好了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=

这是我到目前为止所做的,我想设置,以便输入到考试编号字段的文本正好是4位数字。我遇到了麻烦,因为无论我尝试什么都不起作用,如果有人能帮我一把,甚至在那之后发布完整的代码,那就太好了

    <!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.length==<4) {
            msg+="You must make your exam number exactly 4 digits \n";
            document.ExamEntry.examnumber.focus();
            document.getElementById('examnumber').style.color="red";
            result = false;
    }

    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="text" name="examnumber" maxlength="4"         size="4"></td>
            </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>

报考

您可以使用正则表达式来确保条目正好由四位数字组成,而不包括其他数字:

if (!document.ExamEntry.examnumber.value.match(/^\d{4}$/)) {
  ...
}
^将匹配锚定在字符串的开头(意味着在正则表达式匹配之前不能有任何内容),\d与[0-9]相同,即数字集,{4}表示前面必须有四个数字(在本例中为数字),而$锚定字符串末尾的匹配(意味着正则表达式匹配之后不能有任何内容)


String.prototype.match()在没有匹配项时返回null,否则将返回匹配数组,当用作布尔值时,该数组将计算为true。

使用正则表达式测试4位数字

就目前情况而言,您需要以下产品线:

document.ExamEntry.examnumber.value.length <= 4

document.ExamEntry.examnumber.value.length应该是
document.ExamEntry.examnumber.length==4
?你不是说
=而不是
我应该把这些放在哪里?你现在有
if(document.ExamEntry.examnumber.length==
document.ExamEntry.examnumber.value.length <= 4