单击“提交”后,代码无法识别验证功能

时间:2013-09-26 03:39:53

标签: html jquery-ui submit verify required-field

我正在尝试使用JQuery Ui函数“Verfiy();”在允许表单提交之前验证名称,年龄,国家和日期字段是否已填写。

当用户点击“提交”时应该调用验证功能,但是当我使用以下代码时,无论字段是否已完成,网页都会被定向到“thankyou”页面。它不显示错误消息。

<head>

...

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Wayne Nolting (w.nolting@home.com) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->

<!-- Begin

function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.name.value=="") {
themessage = themessage + " - Name";
}
if (document.form.age.value=="") {
themessage = themessage + " - Age";
}
if (document.form.country.value=="") {
themessage = themessage + " - Country";
}
if (document.form.date.value=="") {
themessage = themessage + " -  Date";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}
//  End -->
</script>

</head>



<body>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>


...

 <input type="submit" value="Submit" onClick="verify()"/></div>
 <input type=reset value="Clear Form"><br>

...

</body>

非常感谢任何见解。

安德鲁

1 个答案:

答案 0 :(得分:0)

在这里,您只需稍微调整一下标记。

<input type="submit" value="Submit" onClick="return verify()"/></div>