如果电子邮件地址无效,我想在屏幕上显示“请输入有效的电子邮件地址”的消息。 body元素的innerHTML语句工作正常,但是我为p元素使用的语句不起作用。innerHTML正在处理body元素,但不是p元素
有一次,当我测试它时,我看到了“请输入一个有效的电子邮件地址”的消息显示,然后我点击了“无效”警告框的确定按钮后,消息就消失了。
的JavaScript:
<script type="text/javascript">
function validateEmail() {
var emailRule = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRule.test(document.forms[0].email.value)) {
document.getElementById("body").innerHTML = "Thank you for signing up for our newsletter! You will recieve a confirmation email shortly.";
setTimeout("window.close()", 3000);
}
else
window.alert("not valid"); //for debugging
document.getElementById("message").innerHTML = "Please enter a valid email address";
}
</script>
HTML:
</head>
<body id="body">
<p>If you sign up for our newsletter you will be entered into a drawing to win free clothes every month!</p>
<br>
<p id="message"> </p>
<form action="" onsubmit="validateEmail();">
<p>Email: <input type="text" name="email"/>
<input type="submit" value="Sign up"/></p>
<br>
<input type="button" value="No thanks" onclick="window.close()"/>
<br>
</form>
<p><a href="privacy.html">Privacy Policy</a></p>
</body>
</html>
你知道'else'不关心缩进,只有大括号'{}'? – 2013-04-25 15:05:48
好点。我会尝试的。然后我再次添加了window.alert语句,因为我有这个问题,所以我认为问题仍然存在。 – user2234760 2013-04-25 15:06:32
@SimonBoudrias if'和'else'在没有{{}'的情况下正常工作,当它的主体中只有一条线时 – Ian 2013-04-25 15:07:00