[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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:

<tr>
<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");
            oTextbox.focus();
            oTextbox.select();
            return false;
      }
      return true;
}

I would rather know what is wrong with this, then have a signifcantly different solution I do not understand.
0
astar666
Asked:
astar666
  • 2
  • 2
1 Solution
 
nabsolCommented:

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

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

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

0
 
astar666Author Commented:
onkeyup did not work, but advanced the cursor
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now