当我尝试使用 Javascript 验证表单时,它会按应有的方式发出警报,但表单被提交,因为即使返回 false,也没有发生任何事情,警报仅在我第一次按下提交按钮时起作用。有人可以告诉我我做错了什么:
Javascript:
function checkForm() {
var name, email, text;
name = document.contact.userName.value;
email = document.contact.email.value;
text = document.contact.textarea.value;
//document.write(name + " " + email + " " + text);
if (name == "" || email =="" || text=="") {
alert("Please fill all fields before submitting!");
return false;
}else if (!looksLikeMail(email)) {
alert("Please enter you real email!");
return false;
}else {
alert("Your feedback was sent, Thank you!");
return true;
}
}
function looksLikeMail(str) {
var lastAtPos = str.lastIndexOf('@');
var lastDotPos = str.lastIndexOf('.');
return ((lastAtPos < lastDotPos) && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2);
> }
HTML:
<form name="contact" action="login" method="POST" onsubmit="checkForm()">
<input type="hidden" name="page" value="feedback">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput1">
Name:
</label>
<input id="name" placeholder="Enter your full name" value="" type="text" name="userName"/>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput2">
Email
</label>
<input id="email" placeholder="Enter your email" value="" type="email" name="email" />
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="textarea1">
Feedback
</label>
<textarea id="textarea" placeholder="Enter your text here" name="textarea"></textarea>
</fieldset>
</div>
<input type="submit" data-inline="true" data-theme="b" data-icon="check" data-iconpos="right" value="Submit" />
</form>