我使用JavaScript创建了一个联系表单,它使用prompt将数据输入变量。然后我需要验证输入的数据,如果它不正确,我需要整个事情停在那里并要求有效的条目。不幸的是,我使用提示从用户获取2个值,我无法进行验证以执行任何操作或更改任何内容。这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title> Support Contact </title>
<link rel="stylesheet" type="text/css" href="stylez.css" />
<script type="text/javascript">
var email = document.getElementById("email");
function checkEmail(email)
{
var reg1 = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg1.test(email) == false) {
alert("Please enter a valid Email address!!")} else
if(reg1.test(email) == true) {
}
}
</script>
<script type="text/javascript">
var email;
var name;
email=prompt("Please enter your primary Email address....", "");
name=prompt("Please enter your name.","");
document.write("Thank you ", name, " someone will contact you within 48 hours!!");
// --></script></head>
<body><h1> Support Contact Page</h1><h2 align="center">
<table width="200" border="1">
<tr>
<td>Available m-sun</td>
<td>Except Holidays</td>
<td><div align="center">Email replies</div></td>
</tr>
</table></h2>
<a href="index.html"><img src="images/300px-Basketball_pictogram.svg.png" width="103"
height="101" alt="home" /></a>
</body>
</html>
根据if条件语句,如果输入的电子邮件无效,我需要停止所有内容。如果变量reg1没有通过测试,我怎么能阻止它?
答案 0 :(得分:2)
在测试失败的测试中,返回false。
if(reg1.test(email) == false) {
alert("Please enter a valid Email address!!")
return false;
} else