chibineku Posted June 29, 2009 Share Posted June 29, 2009 I can't spot the error in this script, which should have been straight forward. I thought maybe some fresh eyes on it couldn't hurt. According to IE and Chrome, there is an unexpected identifier on line 15 (which I'll highlight). <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html;charset=ISO-8859-1" /> <title>Database Sandbox</title> <script type="text/javascript"> var warning = document.getElementById("warning").innerHTML; function verifyUserName() {var userName=document.getElementById("userName").value;var confirmUserName=document.getElementById("confirmUserName").value;if(userName != confirmUserName){ warning="The usernames you have typed do not match";>>>>>LINE 15>>>>>> } elseif (userName =="" OR confirmUserName ==""){ warning="Please fill in all fields"; }}} function verifyPassword() { var password = document.getElementById("password").value; var confirmPassword = document.getElementById("confirmPassword").value; if(password != confirmPassword) { warning="The passwords you have typed do not match"; } elseif(password =="" OR confirmPassword =="") { warning="Please fill in all fields"; } }</script> </head> <body> <form action="/register.php" method="POST"> <p id="warning" name="warning"></p> <p>Please enter your details to register: <br /> <br /> First name:<input type="text" size="20" id="firstName" name="firstName" value="max 20 chars." onfocus="this.value=''" />*<br /> Surname:<input type="text" size="20" id="surname" name="surname" value="max 20 chars." onfocus="this.value=''" />*<br /> Choose a user name:<input type="text" size="20" id="userName" name="userName" value="8-20 chars." onfocus="this.value=''" />*<br /> Confirm user name:<input type="text" size="20" id="confirmUserName" name="confirmUserName" value="8-20 chars." onfocus="this.value=''" onblur="verifyUserName()" />*<br /> Password:<input type="password" size="20" id="password" name="password" value="" onfocus="this.value=''" />*<br /> Confirm password:<input type="password" size="20" id="confirmPassword" name="confirmPassword" value="" onfocus="this.value=''" onblur="verifyPassword()" />*</p> <p> <input type="submit" name="Register" id="Register" value="Register"/> </p> </form> </body> </html> I have tried condensing the last part of the veryUserName function into an elseif, have tried getting rid of the else altogether and embedding another if statement in the first one, have checked that all the brackets and braces match up and everything seems fine.It's probably something tiny and obvious, but I can't spot it for the life of me. Link to comment Share on other sites More sharing options...
Ingolme Posted June 29, 2009 Share Posted June 29, 2009 "OR" is not an operator in Javascript, you use ||. The "AND" operator is written as && in Javascript. Link to comment Share on other sites More sharing options...
chibineku Posted June 29, 2009 Author Share Posted June 29, 2009 Ah, too much time spent in php land. Many thanks, as always. Now if only I could figure out why it is saying it can't read the innerHTML property of null when there is an element with id="warning"... I spend 2 weeks without doing any JavaScript and when I try to do any I've forgotten everything. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 29, 2009 Share Posted June 29, 2009 The line you have getting the HTML of the element gets executed before the element gets created. Javascript gets executed as soon as the browser gets to it, so it executes the line trying to get the HTML of the warning element before it gets to the line that creates the element. Link to comment Share on other sites More sharing options...
chibineku Posted June 30, 2009 Author Share Posted June 30, 2009 Oh man, that used to be my first thing to check! I have such a bad memory that it just falls out of my head if I don't use it all the time.Cheers. Link to comment Share on other sites More sharing options...
Ingolme Posted June 30, 2009 Share Posted June 30, 2009 Ah, too much time spent in php land. Many thanks, as always. Now if only I could figure out why it is saying it can't read the innerHTML property of null when there is an element with id="warning"... I spend 2 weeks without doing any JavaScript and when I try to do any I've forgotten everything.I recommend using && and || in PHP as well. It's more modern and you won't have to memorize two different forms of operators Link to comment Share on other sites More sharing options...
chibineku Posted June 30, 2009 Author Share Posted June 30, 2009 I recommend using && and || in PHP as well. It's more modern and you won't have to memorize two different forms of operatorsYeah, I will do. I use &&, but I can't figure out the keystroke shortcut for the OR symbols and it's a pain going to get it from the character map each time until now didn't know the keyboard shortcut for it. Now I do. w00t. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.