I'm reading through javascript demystified and am currently in chapter 7, Forms and Event Handling. Example 7-1 shows the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml>
<head>
<title>onblur event</title>
<script language="Javascript" type="text/javascript">
<!--
function ValidateEmail(emailaddy)
{
var loc=emailaddy.indexOf('@')
if(loc == -1)
{
alert('you entered an inaccurate email addy.')
}
}
-->
</script>
</head>
<body>
<FORM action="http://www.jimkeogh.com" method="post">
<P>
First Name: <INPUT type="text" name="Fname"/><BR>
Last Name: <INPUT type="text" name="Lname"/><BR>
Email: <INPUT type="text" name="Email" onblur="ValidateEmail (this.value)"/><BR>
<INPUT name="Submit" value="Submit" type="submit"/>
<INPUT name="Reset" value="Reset" type="reset"/>
</P>
</FORM>
</body>
</html>
When I try to load it in a browser, the form loads fine but the browser isn't displaying an alert box. So I don't even know if it is calling the function correctly. Could someone look over this and explain whats going on or what I've done wrong?
It is saying the error is in line 1, and is pointing out ValidateEmail portion of the function, saying it is not defined. see below pic. I'm not sure why it's not defined and could you please point out what I need to do here?
well I don't know how I got it working, all I did was removed the junk in the html tag and the doctype line. So... I'm lost on that part but it is working fine now.
To ziggyism, I'm glad the code I provided is working correctly for you... it's nice to know that it is working properly. I'm quite clear on what the onblur and all the other tags that you can use like it do, what I was really concerned about was the fact that the browser would not execute that code correctly, tho it was copied verbatim from the book. I'm having similar issues with a couple of other examples on into chap 7 of the book.
And to nathanpc, the topic title was to get people to look at the forum post quickly , know that im new, and once you read the question contained in my first forum post then you know exactly what I was asking (tho sorry if it wasn't up to par for you). And i never said i got rid of the html and code tags i said i got rid of the junk in them... aka the parts that were completely unnecessary.
well I don't know how I got it working, all I did was removed the junk in the html tag and the doctype line. So... I'm lost on that part but it is working fine now.
By removing the extra text in the <html> you removed the source of the error - missing closing "
.....tho it was copied verbatim from the book. I'm having similar issues with a couple of other examples on into chap 7 of the book.....
Imo code in text or any books should not be blindly taken as 100% correct. There is always a possibility if typos or misprints - albeit these are rare.
Depending on the problem, sources of errors in problematic html code can be found by putting the hml code through the w3c validator at:
Bookmarks