0

For some reason, I can't get the alert box (added as a test) to display properly. The function doesn't appear to execute at all. What are your thoughts of what I'm doing wrong?

My code is located here btw w/ the result. Thanks for your input in advance!

stanigator
  • 10,768
  • 34
  • 94
  • 129

5 Answers5

5

An obvious problem is bad syntax due to quotes in string constants. E.g.,

h[0] = "<span style="color:red;">Please type a name!</span>";

should be

h[0] = "<span style=\"color:red;\">Please type a name!</span>";

or

h[0] = '<span style="color:red;">Please type a name!</span>';
Thorn
  • 340
  • 2
  • 4
3
  1. var h = new Array(); should be just var h = [];.

  2. h[0] = "<span style="color:red;">Please type a name!</span>"; is invalid syntax - how do you expect it to know which " is meant to terminate the string? Use style='color:red' (single quotes).

  3. for(i in x) should not be used.

  4. As was said, bind to the form's submit event instead of button click. And don't use inline handlers for that matter, use event listeners.

  5. If there were errors, your handler needs to return false; to stop form submission from going to the server.

  6. Read gdoron's answer and use JSLint.

Community
  • 1
  • 1
DCoder
  • 12,962
  • 4
  • 40
  • 62
  • Re: 2, I've read your link. I still don't understand why I should avoid using `for (i in x)` though. – stanigator Apr 29 '12 at 06:07
  • From that link: "The purpose of the for-in statement is to enumerate over object properties, this statement will go up in the prototype chain, enumerating also inherited properties, thing that sometimes is not desired. Also, the order of iteration is not guaranteed by the spec." - iterating over an array's contents in strict order is not what it's for. – DCoder Apr 29 '12 at 06:13
1

You have many many errors in your code!
After testing your code with JSLint I got those errors:

Error: Problem at line 2 character 22: Use the array literal notation []. var x = new Array();

Problem at line 9 character 22: Use the array literal notation []. var h = new Array();

Problem at line 10 character 24: Missing semicolon. h[0] = "

Problem at line 11 character 12: Label 'color' on red statement. color: red;

Problem at line 11 character 12: Expected an assignment or function call and instead saw an expression. color: red;

Problem at line 12 character 5: Expected an assignment or function call and instead saw an expression. ">Please type a name!";

Problem at line 13 character 24: Missing semicolon. h[1] = "

Problem at line 14 character 12: 'color' is already defined. color: red;

Problem at line 14 character 12: Label 'color' on red statement. color: red;

Problem at line 14 character 12: Expected an assignment or function call and instead saw an expression. color: red;

Problem at line 15 character 5: Expected an assignment or function call and instead saw an expression. ">You must type a last name!";

Problem at line 16 character 24: Missing semicolon. h[2] = "

Problem at line 17 character 12: 'color' is already defined. color: red;

Problem at line 17 character 12: Label 'color' on red statement. color: red;

Problem at line 17 character 12: Expected an assignment or function call and instead saw an expression. color: red;

Problem at line 18 character 5: Expected an assignment or function call and instead saw an expression. ">You must type a valid email address!";

Problem at line 19 character 24: Missing semicolon. h[3] = "

Problem at line 20 character 12: 'color' is already defined. color: red;

Problem at line 20 character 12: Label 'color' on red statement. color: red;

Problem at line 20 character 12: Expected an assignment or function call and instead saw an expression. color: red;

Problem at line 21 character 5: Expected an assignment or function call and instead saw an expression. ">You must type a password!";

Problem at line 22 character 24: Missing semicolon. h[4] = "

Problem at line 23 character 12: 'color' is already defined. color: red;

Problem at line 23 character 12: Label 'color' on red statement. color: red;

Problem at line 23 character 12: Expected an assignment or function call and instead saw an expression. color: red;

Problem at line 24 character 5: Expected an assignment or function call and instead saw an expression. ">You must confirm the password!";

Problem at line 26 character 26: Use the array literal notation []. var divs = new Array("mname", "mlname", "memail", "mpassword", "mconfirm");

Problem at line 26 character 26: Use the array literal notation []. var divs = new Array("mname", "mlname", "memail", "mpassword", "mconfirm");

Problem at line 26 character 26: Stopping, unable to continue. (72% scanned).

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 2
    Those aren't all errors, they're suggestions, your listing all these issues doesn't help figure out the real problems (The unescaped quotes) – Ruan Mendes Apr 29 '12 at 06:05
  • @JuanMendes. Right, I meant to show him that testing your code with jslint face by face can give you the answer. (+ best practice.) – gdoron Apr 29 '12 at 06:13
1

There are errors in these lines in "" change to ''

     h[0] = "<span style='color:red;'>Please type a name!</span>";
     h[1] = "<span style='color:red;'>You must type a last name!</span>";
     h[2] = "<span style='color:red;'>You must type a valid email address!</span>";
     h[3] = "<span style='color:red;'>You must type a password!</span>";
     h[4] = "<span style='color:red;'>You must confirm the password!</span>";
Bob
  • 1,489
  • 1
  • 16
  • 28
0

The main problem is actually in the way you're defining your error spans, eg.:

h[0] = "<span style="color:red;">Please type a name!</span>";

This is causing a syntax error because the quotes around the style are unescaped, causing them to end the string. That breaks the entire function. Escaping them will get you a long way:

h[0] = "<span style=\"color:red;\">Please type a name!</span>";

But you will also need to check keune's answer.

Rick
  • 1,268
  • 9
  • 8