0

我在使用 JavaScript 时遇到一些问题,在提交之前检查我的表单是否完成。这是我的表单代码:

<form class="myform" accept-charset="UTF-8" onsubmit="return validateForm();" action="https://Autorespondercode.com" method="POST">

<div class="front-name"><input class="form-name" id="inf_field_FirstName" type="text" name="inf_field_FirstName" placeholder="First Name" /></div>


<div class="front-email"><input class="form-email" id="inf_field_Email" type="text" name="inf_field_Email" placeholder="Email" /></div>


<input style="background-color: #fc8f12;" type="submit" value="Subscribe" />


</form>

JavaScript:

function validateForm() { 
    var a=document.forms["myform"]["inf_field_FirstName"].value; 
    var b=document.forms["myform"]["inf_field_Email"].value; 
    if (a==null || a=="" || a=="First Name")
    {   alert("Please enter your First Name!");   
    return false;   }     
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;    
    if (document.myform.inf_field_Email.value.search(emailRegEx) == -1)  
    {           alert("Please enter a valid email address.");               
    return false;    } }

感谢任何帮助/提示。

4

2 回答 2

2

将您的脚本替换为:

    function validateForm() {
        var a = document.getElementById('inf_field_FirstName').value;
        var b = document.getElementById('inf_field_Email').value;
        if (a == null || a == "" || a == "First Name") {
            alert("Please enter your First Name!");
            return false;
        }
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        if (document.getElementById('inf_field_Email').value.search(emailRegEx) == -1) {
            alert("Please enter a valid email address.");
            return false;
        }
    }

浏览器无法通过以下方式获得价值:

  var a=document.forms["myform"]["inf_field_FirstName"].value; 
  var b=document.forms["myform"]["inf_field_Email"].value; 
于 2013-11-11T06:42:35.333 回答
0

你能试试吗,你没有提到你的表格myform名称form tag

   <form name="myform" class="myform" accept-charset="UTF-8" onsubmit="return validateForm();" action="https://Autorespondercode.com" method="POST">
于 2013-11-11T06:45:07.667 回答