javascript validation of html form element for non-blank

I read a javascript book and now I am trying to apply it.
Here I simply want to force entry of something on a form text box.  I had high hopes, but I can tab right through the form.

Here is the html:

<td>Enter Userid</td><td><input type="text" name="userid" size="15" maxlength="15" onblur="MustEnter(this)"></td></tr><tr>
<td>Select Password</td><td><input type="password" name="password" size="15" maxlength="15" onblur="MustEnter(this)"></td></tr><tr>
<td>City of Birth</td><td><input type="text" name="cityofbirth" size="15" maxlength="15" onblur="MustEnter(this)"></td></tr><tr>
<td>Favorite color</td><td><input type="text" name="mothersmaidenname" size="15" maxlength="15" onblur="MustEnter(this)"></td></tr>
<tr><td>First name</td><td><input type="text" name="firstname" size="20" maxlength="20" onblur="MustEnter(this)"></td></tr>
<tr><td>Last name</td><td><input type="text" name="lastname" size="30" maxlength="30" onblur="MustEnter(this)"></td></tr>
<tr><td>Email</td><td><input type="text" name="email" size="40" maxlength="50" onblur="MustEnter(this)"></td></tr>

Here is the javascript from a js file that is loaded and working on another function:

function MustEnter(oTextbox) {
      if oTextbox.value.length==0 {
            alert("Must enter");
            return false;
      return true;

I would rather know what is wrong with this, then have a signifcantly different solution I do not understand.
Who is Participating?

if oTextbox.value.length==0
if (oTextbox.value.length==0)

By Nab
astar666Author Commented:
that helps.  I now get the alert.  However the cursor advances to the next text box.  Please advise.

onblur is called whenever focus is transfered to other textbox i.e. cursor advences to other text box.
May be this can cause an infinite loop. So its not suggested that you use onblur. Try using onkeyup instead of onblur. Or use a <form onsubmit="return validate()" > to "validate" at submission of form.

By Nab

astar666Author Commented:
onkeyup did not work, but advanced the cursor
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.