Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
hugo

Pg 157-158:code Isn't Working-Help

Recommended Posts

I'm having trouble with the following javascript code:

 

function process()
{
    'use strict';
    var okay=true;
    
    var email = document.getElementById('email');
    var comments=document.getElementById('comments');
    
    
    
    if(!email || !email.value
    || (email.value.length < 6)
    || (email.value.indexOf('@')= = -1))
    {
    okay=false;
    alert('Please enter a valid email address');
    }
    
    if(!comments || !comments.value
    || (comments.value.indexOf('<') != -1)) {
    okay=false;
    alert('Please enter your comments without any HTML!');
    }
    
    return okay;
}

function init(){
    'use strict';
    document.getElementById('conForm').onsubmit = process;
    }
window.onload = init;

 

Problem:

After entering a valid email address in the HTML form, I go on to enter a < character in the comments box;the second conditional does not fire off after submitting the form.

 

I've already checked the errata and downloaded the .zip file for the chapter and compared line by line. I've typed the code correctly as shown in the book.

 

For testing, I commented out the first conditional using /* and */ and afterwards, I am able to get the second conditional to activate after typing in a < character in the text area.

 

Question:

How would I get both conditionals to function at the same time? That is, after entering a valid email address, but entering a prohibited character in the comments box?

Share this post


Link to post
Share on other sites

The following is incorrect:

email.value.indexOf('@')= = -1

You cannot have a space between the two equals signs.

Also, your code and formatting are pretty sloppy to begin with.

I'd try straightening it all up so that it's easier to read and silly errors are less likely to happen.

Share this post


Link to post
Share on other sites

Thank you very much! The code works now. :rolleyes:

 

Can you recommend a good source of reading on proper and/or non-sloppy code formatting?

 

I've found several sources online after running a google search, but am not sure which one to trust.

 

Your advice is highly appreciated.

 

Thanks in advance.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...