我的JavaScript表单验证不工作
My JavaScript Form Validation is Not Working
我试图验证我的表单中的三个字段。表单是一个简单的联系人表单和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.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>
相关文章:
- 自定义验证器不工作
- JavaScript表单验证-“;“错误”;不按需要工作
- javascript正则表达式验证未按预期工作
- 表单验证工作不正常,在不检查条目的情况下继续
- 为什么Angularjs验证don't与输入[type=“number”]一起工作
- 表单验证在接近尾声时停止工作
- 在键上验证正在工作,但在按键时它不起作用
- 表单验证在 HTML 中工作的 Visualforce 中不起作用
- CSS 类在验证时只工作一次
- 在角度验证中禁用 ng 无法按预期工作
- 为什么我的 Javascript 表单验证无法正常工作
- RubyonRails中的JavaScript表单验证-让JS正常工作,但我该把它放在哪里
- 添加Jquery验证后,Jquery日期选择器无法工作
- 输入字段验证的键代码函数在chrome中没有按预期工作
- 使用javascript的十进制验证未按预期工作
- 我正在尝试使用jqueryValidation引擎插件验证注册表单,有些验证无法正常工作
- 主干.js:在将模型添加到集合之前不进行工作验证
- 不;对传递给指令AngularJS的表单进行工作验证
- 不能正常工作验证引导jalali日期拾取器
- 为什么这个JQuery点击功能不工作?(验证按钮)