Jump to content

Unexpected Identifier...


chibineku
 Share

Recommended Posts

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.

Edited by chibineku
Link to comment
Share on other sites

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

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

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

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

I recommend using && and || in PHP as well. It's more modern and you won't have to memorize two different forms of operators
Yeah, 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...