javascript - 表单验证问题

标签 javascript html forms validation

我正在尝试使用 JavaScript 验证一个简单的表单,但无法让脚本进行验证。当按下“提交”按钮时,不会运行任何验证。如果其中一个框留空,则不会弹出错误消息来通知用户。不过我没有看到任何不合适的地方。我想知道是否是提交按钮不起作用?有人可以帮忙吗?

<DOCTYPE html PUBLIC "-//W3C//DTD  XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <head>
  <meta http-equiv="Content-Type"  content="text/html; charset=utf-8" />
  <title>LASTNAME: Warner Form Validation</title>
  <script type="text/javascript">
  function btnSubmit_onclick() {
     var myForm = document.formContact;
     if(myForm.txtAge.value == "" || myForm.txtName.value == "") {
        alert.("Please complete all of the fields.");
        if(myForm.txtName.vale == ""){
           myForm.txtName.focus();
        } else {
           myForm.txtAge.focus();
     } else{
        alert("Thank you for completing the form"+myForm.txtName.value);
     }
  }
  function txtAge_onblur() {
     var txtAge = document.formContact.txtAge;
     if(isNaN(txtAge.value) == true) {
        alert("Please Enter a Valid Age");
        txtAge.select();
    }
  function txtName_onchange(); {
     window.status = "Hi" + document.form1.txtName.value;
     docment.getElementById('greeting').innerHTML = "Hi" + document.frm1.txtName.value;
  }
  </script>
  </head>
  <body>
  <form action="" id="formContact" method="post" onsubmit="return validate();" name="formContact">
     <p>Name:<br />
        <input type="text" name="theName"><span class="err" id="nameErr"></span>
     </p>
     <p>Age:<br />
        <input type="text" name="theAge"><span class="err" id="ageErr"></span>
     </p>
     <p>
     <p>How may we contact you?<br>
        <input type="radio" name="contact" value="email">Email<br>
        <input type="radio" name="contact" value="no contact">No Contact<br>
     </p>
     <p><input type="button" value="Submit" name="btnSubmit" onclick="btnSubmit_onclick></p>
  </form>
  <span id="results"></span>
  </body>
</html>

最佳答案

你的代码有很多拼写错误。这有效。

<DOCTYPE html PUBLIC "-//W3C//DTD  XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8" />
<title>LASTNAME: Warner Form Validation</title>
<script type="text/javascript">
function btnSubmit_onclick() {
 var myForm = document.formContact;
 if(myForm.txtAge.value == "" || myForm.txtName.value == "") {
    alert("Please complete all of the fields.");
    if(myForm.txtName.vale == ""){
       myForm.txtName.focus();
    } else {
       myForm.txtAge.focus();
    }
 } else{
    alert("Thank you for completing the form"+myForm.txtName.value);
 }
}
function txtAge_onblur() {
 var txtAge = document.formContact.txtAge;
 if(isNaN(txtAge.value) == true) {
    alert("Please Enter a Valid Age");
    txtAge.select();
}
function txtName_onchange() {
 window.status = "Hi" + document.form1.txtName.value;
 docment.getElementById('greeting').innerHTML = "Hi" + document.frm1.txtName.value;
}
}
</script>
</head>
<body>
<form action="" id="formContact" method="post" onsubmit="return validate();" name="formContact">
 <p>Name:<br />
    <input type="text" name="txtName"><span class="err" id="nameErr"></span>
 </p>
 <p>Age:<br />
    <input type="text" name="txtAge"><span class="err" id="ageErr"></span>
 </p>
 <p>
 <p>How may we contact you?<br>
    <input type="radio" name="contact" value="email">Email<br>
    <input type="radio" name="contact" value="no contact">No Contact<br>
 </p>
 <p><input type="button" value="Submit" name="btnSubmit" onclick="btnSubmit_onclick()"></p>
</form>
<span id="results"></span>
</body>
</html>

但请注意,您尚未在任何地方使用 txtAge_onBlur 或 txtName_onChange 函数。我还建议在年龄字段中添加数字检查。

关于javascript - 表单验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038178/

相关文章:

Javascript - 执行多个不同的 window.onscroll

javascript - 在同一页面中克隆 Bootstrap-select

javascript - 取消选中所有其他复选框

PHP、MySQL。要求人们使用 key 注册

javascript - 给定一棵树,一个开始叶子和一个结束叶子,将它分成最多 3 个部分

javascript - jQuery 多重显示隐藏在一种形式中

html - 从画廊加载单张照片,直到我转到另一页 [滑动 JS]

javascript - 循环结束后重启 jQuery.each()

javascript - 当我们点击复选框时如何获取一个或多个数据?

vba - 如何使用 VBA 在 MS Access 中添加新记录?