我不知道为什么 javascript 不起作用。不显示警报消息。这是一个简单的 html 文件,用于验证用于将书籍添加到数据库的表单。在点击提交按钮时,页面将自己定向到以下链接,而不显示警报窗口。请帮帮我。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add Book</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/default.css" />
</head>
<body id="index_page">
<div id="wrapper">
<div id="header">
<h1 align="left">Library</h1>
</div>
<ul id="navigation">
<li id="index"><a href="http://localhost/UID/FAQ.php">FAQ</a></li>
</ul>
<div id="content">
<div id="main_content">
<h2>Add book</h2>
<h4>Enter details of the book</h4>
<FORM action="add_book_action.php" method="post" >
<TABLE WIDTH="50%" >
<TR>
<TH width="50%">Book ID:</TH>
<TD width="50%"><INPUT TYPE="text" name="id" id="book_id"></TD>
</tr>
<TR>
<TH width="50%">Book Name:</TH>
<TD width="50%"><INPUT TYPE="text" name="name" id="book_name"></TD>
</tr>
<TR>
<TH width="50%">Book Author:</TH>
<TD width="50%"><INPUT TYPE="text" NAME="author" id="book_author"></TD>
</tr>
<TR>
<TH width="50%">Year of Publication:</TH>
<TD width="50%"><INPUT TYPE="text" NAME="pub" id="book_year"></TD>
</tr>
<TR>
<TH></TH>
<TD width="50%"><INPUT TYPE="submit" Value="Submit" onclick="return validate_fun()"></TD>
</tr>
</TABLE>
</FORM>
</div>
<p id="footer"><a href="http://localhost/UID/UID.php">@AntonyAjay,2012</a></p>
</div>
<script language="javascript">
function validate_fun()
{
var x=document.getElementById("book_id").value;
if(x==""||isNaN(x))
{
alert("Book Id should be numeric");
return false;
}
var x=document.getElementById("book_name").value;
var regex = /^[a-zA-Z ]*$/;
if(!(regex.test(x)))
{
alert("Book Name should be only characters and spaces");
return false;
}
var x=document.getElementById("book_author").value;
var regex = /^[a-zA-Z ]*$/;
if(!(regex.test(x)))
{
alert("Author Name should be only characters and spaces");
return false;
}
}
var x = document.getElementById("book_year").value;
var regExp = /^([1][6-9][0-9][0-9]|[2][0][01][0-9]\/|-([1-9]|[1][0-9]|[2][0-9]|[3][01])\/|-([1-9]|[1][012])$/;
if(!(regExp.test(x)))
{
alert("Enter date in yyyy-mm-dd format");
return false;
}
}
</script>
</body>
</html>