我在使用 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; } }
感谢任何帮助/提示。