我的 JavaScript 遇到的问题是,当我为空白表单选择“发送”按钮时,它会告诉我要完成哪些字段(我想要的)。选择“确定”后,它要求我“请输入有效的电子邮件地址”。在另一个窗口。
如果有人选择“发送”而不填写表格,任何人都可以帮助我消除第二个窗口的功能和逻辑吗?我是否需要为“输入有效的电子邮件地址”创建一个新功能?
这是代码:
javascript
function checkforblank() {
var errormessage = "";
if (document.getElementById('fname').value =="") {
errormessage += "enter your first name \n";
}
if (document.getElementById('lname').value =="") {
errormessage += "enter your last name \n";
}
if (document.getElementById('email').value =="") {
errormessage += "enter your email \n";
}
if (document.getElementById('confirmEmail').value =="") {
errormessage += "confirm your email \n";
}
if (errormessage != "") {
alert(errormessage);
return false;
}
};
function verifyEmail() {
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myForm.email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
}
else if (document.myForm.email.value != document.myForm.confirmEmail.value) {
alert("Email addresses do not match. Please retype them to make sure they are the same.");
}
else {
alert("Thank you for your interest!");
status = true;
}
return status;
}
function confirmEmailAddresses() {
checkforblank();
verifyEmail();
}
html
<div id="content">
<form name="myForm" action="#" method="get" enctype="application/x-www-form-urlencoded" onsubmit="">
<table width="377" height="96">
<tr>
<td style="text-align: right">First Name:</td>
<td><label for="FirstName"></label>
<input type="text" name="fname" id="fname"></td>
</tr>
<tr>
<td style="text-align: right">Last Name:</td>
<td><label for="LastName"></label>
<input type="text" name="lname" id="lname"></td>
</tr>
<tr>
<td style="text-align: right">E-mail:</td>
<td><input type="email" name="email" id="email"></td>
</tr>
<tr>
<td style="text-align: right">Confirm E-mail:</td>
<td><input type="email" name="confirmEmail" id="confirmEmail"></td>
</tr>
</table>
<input type="submit" value="Send" onClick="confirmEmailAddresses()"><input type="reset" value="Reset Form">
</form>
</div>
我对 JavaScript 很陌生,所以请尽可能简单!:) 谢谢。