I don't know why the javascript doesn't work. The alert messages aren't displayed. This is a simple html file to validate a form that is used to add a book to a database. On hitting the submit button, the page directs itself to the following links without diplaying the alert window.Please help me out.
<!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>
Syntax Error: unexpected token }
. Open your browser's debugging tools (F12 in most browsers), look at the errors it reports. (Edit: and learn to indent your code, or get an IDE that can do it for you. The error will be obvious.) – DCoder Dec 23 '12 at 9:12too localised
since it's unlikely anyone will 1) encounter the same issue (syntax error) 2) under the same circumstances and 3) will not find it by debugging. – Jan Dvorak Dec 23 '12 at 9:15Syntax Error
s fall under "too localized". – DCoder Dec 23 '12 at 9:16