我的JavaScript表单验证无效
我试图验证表单中的三个字段。该表单是一个简单的联系我表单和JavaScript。下面是验证的javascript代码:我的JavaScript表单验证无效,javascript,html,Javascript,Html,我试图验证表单中的三个字段。该表单是一个简单的联系我表单和JavaScript。下面是验证的javascript代码: <script type="text/javascript"><!-- function validateForm() { with (document.contact-form) { var alertMsg = "The following REQUIRED fields\nhave been left empty:\n"; if (name.va
<script type="text/javascript"><!--
function validateForm() {
with (document.contact-form) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (name.value == "") alertMsg += "\nNAME IS REQUIRED";
if (email.value == "") alertMsg += "\nEMAIL IS REQUIRED";
if (phone.value == "") alertMsg += "\nPHONE IS REQUIRED";
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
} } }
// --></script>
我的表单HTML代码是:
<form id="contact-form" name="contact-form" action="form.php" method="post"
onsubmit="return validateForm()">
<input id="name" type="name" name="name" value="NAME"
onfocus="if (this.value=='NAME') this.value='';" onblur="if (this.value=='')
this.value='NAME';"/><br />
<input id="email" type="email" name="email" value="EMAIL" onfocus="if
(this.value=='EMAIL') this.value='';" onblur="if (this.value=='')
this.value='EMAIL';" /><br />
<input id="phone" type="text" name="phone" value="PHONE" class="phone" onfocus="if
(this.value=='PHONE') this.value='';" onblur="if (this.value=='')
this.value='PHONE';" /><br />
<input type="text" name="event-location" value="EVENT LOCATION" onfocus="if
(this.value=='EVENT LOCATION') this.value='';" onblur="if (this.value=='')
this.value='EVENT LOCATION';" /><br />
<input type="text" name="event-date" value="EVENT DATE" onfocus="if
(this.value=='EVENT DATE') this.value='';" onblur="if (this.value=='')
this.value='EVENT DATE';" /><br />
<input type="text" name="event-time" value="EVENT TIME" onfocus="if
(this.value=='EVENT TIME') this.value='';" onblur="if (this.value=='')
this.value='EVENT TIME';" /><br />
<input type="text" name="message" value="MESSAGE" class="message" onfocus="if
(this.value=='MESSAGE') this.value='';" onblur="if (this.value=='')
this.value='MESSAGE';" /><br />
<input type="submit" name="send" value="SEND" />
</form>
任何帮助都将不胜感激
谢谢你试试这个
<script type="text/javascript"><!--
function validateForm() {
var mytable = document.getElementById('contact-form');
with (mytable) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (name.value == "" || name.value == "NAME") alertMsg += "\nNAME IS REQUIRED";
if (email.value == "" || email.value == "EMAIL") alertMsg += "\nEMAIL IS REQUIRED";
if (phone.value == "" || phone.value == "PHONE") alertMsg += "\nPHONE IS REQUIRED";
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
} } }
// --></script>
或者更直接一点
<script type="text/javascript"><!--
function validateForm() {
var mytable = document.getElementById('contact-form');
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (document.getElementById('name').value == "") alertMsg += "\nNAME IS REQUIRED";
if (document.getElementById('email').value == "") alertMsg += "\nEMAIL IS REQUIRED";
if (document.getElementById('phone').value == "") alertMsg += "\nPHONE IS REQUIRED";
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
} }
// --></script>
不幸的是,这不起作用。我把它放在html文档的开头。在我的HTML中还有'onfocus=“if(this.value=='NAME')this.value='';onblur=“if(this.value='')this.value='NAME';”,这是问题的原因吗?感谢用户,这就是导致此问题的原因。如果删除onblur代码并在光标位于name字段时单击submit,它将提交。可能是,我将删除所有onFocus和onblur语句,然后尝试您的代码。现在,您可能会将它们设置为一个值。是的,但我想要它提供给我的功能。我调整了javascript以考虑您在onBlur中设置的默认值。那应该对你有用