www.webdeveloper.com
Results 1 to 10 of 10

Thread: Im just learning Javascript and..

  1. #1
    Join Date
    May 2010
    Posts
    5

    Im just learning Javascript and..

    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?

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    All that is needed is some basic Debugging 101

    The Firefox error console tells you what the error is and the line number its in.

    You have an obvious syntax error.

  3. #3
    Join Date
    May 2010
    Posts
    5
    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?

    http://i33.photobucket.com/albums/d9...lurerror-1.jpg

  4. #4
    Join Date
    May 2010
    Posts
    5
    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.

  5. #5
    Join Date
    May 2010
    Location
    Brazil
    Posts
    50
    Please use the html and the code tags, also put better topic titles, that really describe what's you're question.

    Keep the forums clean and organized.

  6. #6
    Join Date
    Apr 2010
    Posts
    10
    Hi techdead,

    The script you provided works fine.

    You seem to be confused on what an onblur event is.

    A user clicks on the form field, and it focuses on that field. This is called onfocus.

    The user then clicks out of the form field, and it is not longer focused. This is called onblur.

    So, enter an email address into the form field, and then click out of the field. Your alert box should appear.

  7. #7
    Join Date
    May 2010
    Posts
    5
    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.

  8. #8
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by techdead View Post
    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 "

    In your original post:

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml>
    should have been

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">

  9. #9
    Join Date
    May 2010
    Posts
    5
    Aha! Thank you Tirna! I knew it would be something like that... I had been so annoyed over that.

  10. #10
    Join Date
    Mar 2010
    Posts
    2,803
    no problem - glad you sorted it out.

    .....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&#37; 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:

    http://validator.w3.org/#validate_by_upload

    anyway, just some food for thought

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles