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

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

任何帮助都会非常感谢

最佳答案

  1. getElementById 而不是 getelementById
  2. 使用 fullName 代替保留字名称
  3. 不要使用与表单名称相同的 ID - IE 会混淆
  4. 不要使用提交按钮的onclick。而是使用表单 onsubmit
  5. 您可能不被允许 POST 到 HTML 文件
  6. HTML5 支持 type="number"而不是 "integer"
  7. 将属性value添加到每个字段

这是我的建议 - 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
  }
}

关于javascript - 验证文本框,以便必须输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14376882/

相关文章:

javascript - 需要限制用户检查每个内表中的连续复选框

Javascript onclientclick 有帮助吗?

javascript - 如何在父元素中专门调用一个ID并忽略具有相同ID的所有其他元素(使用纯JS)?

javascript - 在输入框中设置 "minimum"字符输入抛出 javascript

java - 在 JPA/JAX-RS Web 服务中验证 JAXBElement

php - 检查是否存在任何错误消息并在 laravel 中显示所有错误消息

validation - 为什么 <o :validateAll> behave different than other validators?

javascript - AJAX 带滚动条的水平滚动

javascript - 使用 $.each 迭代对象数组

validation - Magento 仅验证表单的某些字段