Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证文本框,以便必须输入值_Javascript_Validation - Fatal编程技术网

Javascript 验证文本框,以便必须输入值

Javascript 验证文本框,以便必须输入值,javascript,validation,Javascript,Validation,嗨,我试图让这段代码来验证文本框,所以它们必须包含一个值;如果他们没有,我希望出现一条消息,说明字段不能为空。以下是我编写的代码: <head> <title>Exam entry</title> <script language="javascript" type="text/javascript"> function validateForm() { var result = true; var msg="";

嗨,我试图让这段代码来验证文本框,所以它们必须包含一个值;如果他们没有,我希望出现一条消息,说明字段不能为空。以下是我编写的代码:

<head>
<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.gotElementById('name').style.color="red";
        result = false;
    }

        if (document.ExamEntry.subject.value=="") {
        msg+="You must enter the subject \n";
        document.ExamEntry.subject.focus();
        document.gotElementById('subject').style.color="red";
        result = false;
    }

        if (document.ExamEntry.examno.value=="") {
        msg+="You must enter an examination number \n";
        document.ExamEntry.examno.focus();
        document.gotElementById('examno').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="examno">Examination Number</td>
        <td><input type="integer" name="examno" /></td>
    </tr>

<tr>
    <td><input type="submit" name="Submit" value="Submit" onclick="return validationFrom();" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
</tr>

</table>
</form>
</body>

报考
函数validateForm(){
var结果=真;
var msg=“”;
if(document.ExamEntry.name.value==“”){
msg+=“您必须输入您的姓名\n”;
document.ExamEntry.name.focus();
document.gotElementById('name').style.color=“red”;
结果=假;
}
if(document.ExamEntry.subject.value==“”){
msg+=“您必须输入主题\n”;
document.ExamEntry.subject.focus();
document.gotElementById('subject').style.color=“red”;
结果=假;
}
if(document.ExamEntry.examno.value==“”){
msg+=“您必须输入考试编号\n”;
document.ExamEntry.examno.focus();
document.gotElementById('examno').style.color=“red”;
结果=假;
如果(msg==“”){
返回结果;
}
{
警报(msg)
返回结果;
}
}
报考表
名称
主题
考试号码

任何帮助都很好,谢谢

属性值不能直接用于元素,您应该始终使用:

element.getAttribute('value')
这将获得value属性

希望这有帮助

  • getElementById而非gotelementById
  • 使用全名而不是保留字名
  • 不要使用与表单名称相同的ID-IE将被混淆
  • 不要使用提交按钮的onclick,而是使用表单onsubmit
  • 您可能不被允许发布到HTML文件
  • HTML5支持type=“number”而不是“integer”
  • 向每个字段添加属性
  • 以下是我的建议-

    我已更改标签ID,并将名称更改为fullName

    function setError(fld,id,msg) {
    
       if (fld.value=="") {
         document.getElementById(id).className="error";
         return msg;
       }
       document.getElementById(id).className="normal";
       return "";
    
    }
    window.onload=function() {
      document.getElementById("ExamEntry").onsubmit=function() {
    
        var msg,msgs=[],focusField="";
        msg=setError(this.fullName,"fullNameLabel","You must enter your name");
        if (msg) { msgs.push(msg); focusField = this.fullName;}
        msg=setError(this.subject,"subjectLabel","You must enter the subject");
        if (msg) {  msgs.push(msg); focusField = focusField || this.subject;}
    
        var examno = this.examno;
        msg = setError(examno,"examnoLabel","You must enter an examination number");
        if (msg) { msgs.push(msg); focusField = focusField || examno;}
        else {
          if (isNaN( examno.value) ||  examno.value.length<4) {
            msgs.push("You must enter a valid examination number");
            document.getElementById("examnoLabel").className="error"
            focusField = focusField || this.examno;
          }
        }
        if(focusField) {
          alert(msgs.join("\n"));
          focusField.focus();
          return false;
        }
        return true; // allow submission
      }
    }
    
    函数设置错误(fld、id、msg){
    如果(fld.value==“”){
    document.getElementById(id).className=“error”;
    返回味精;
    }
    document.getElementById(id).className=“正常”;
    返回“”;
    }
    window.onload=function(){
    document.getElementById(“ExamEntry”).onsubmit=function(){
    var msg,msgs=[],focusField=“”;
    msg=setError(this.fullName,“fullnamelab”,“您必须输入您的姓名”);
    if(msg){msgs.push(msg);focusField=this.fullName;}
    msg=setError(this.subject,“subjectLabel”,“您必须输入主题”);
    if(msg){msgs.push(msg);focusField=focusField | | this.subject;}
    var examno=this.examno;
    msg=setError(examno,“examnoLabel”,“您必须输入检查编号”);
    if(msg){msgs.push(msg);focusField=focusField | | examno;}
    否则{
    
    如果(isNaN(examno.value)| | examno.value.length如果使用保留字命名输入元素,例如
    name
    ,则会遇到问题。代码:

    document.ExamEntry.name.value
    
    将给您带来问题,因为
    ExamEntry
    表单已经有一个名为
    name
    的属性。您应该重命名字段,或者使用:

    document.ExamEntry.elements['name'].value
    

    你错过了一个}在第三个if的末尾。还有,有很多代码重复,试着在函数中进行抽象。嗯?你是如何得出这个结论的?非常感谢,因为你可以告诉我我是一个新手!我现在可以看到我错在哪里了,非常感谢你的帮助!如果我要扩展考试编号字段以验证考试编号至少为4 n长度是多少?我该怎么做?