验证文本框,以便输入值

时间:2013-01-17 10:23:57

标签: javascript validation

您好我正在尝试使用此代码验证文本框,因此它们必须包含值;如果他们不要我想要一条消息,说明这些字段不能为空。这是我写的代码:

<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>

任何帮助都会非常感谢

3 个答案:

答案 0 :(得分:2)

  1. getElementById而不是gotelementById
  2. 使用fullName而不是保留字名称
  3. 不要使用与表单名称相同的ID - IE会被混淆
  4. 不要使用提交按钮的onclick。而是使用表单onsubmit
  5. 您可能不会被允许POST到HTML文件
  6. HTML5支持type =&#34; number&#34;不是&#34;整数&#34;
  7. 将属性value添加到每个字段
  8. 以下是我的建议 - DEMO

    我更改了标签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
      }
    }
    

答案 1 :(得分:1)

如果使用保留字命名输入元素,例如name,则会遇到问题。代码:

document.ExamEntry.name.value

会导致您遇到问题,因为ExamEntry表单已经有一个名为name的属性。您应该重命名字段,或使用:

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

答案 2 :(得分:0)

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

element.getAttribute('value')

这将为您提供值属性。

希望这有帮助。