hugo 0 Posted April 2, 2014 Report Share Posted April 2, 2014 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? Quote Link to post Share on other sites
HartleySan 826 Posted April 2, 2014 Report Share Posted April 2, 2014 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. Quote Link to post Share on other sites
hugo 0 Posted April 2, 2014 Author Report Share Posted April 2, 2014 Thank you very much! The code works now. 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. Quote Link to post Share on other sites
HartleySan 826 Posted April 3, 2014 Report Share Posted April 3, 2014 There are a variety of tools/books/resources, but my personal favorite is JSLint: http://www.jslint.com/ While extremely strict, I recommend following it to the tee. It will make you a very good JS coder, and coder in general. 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.