javascript validation of html form element for non-blank

Posted on 2006-04-22
Last Modified: 2008-01-09
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.
Question by:astar666
    LVL 6

    Accepted Solution


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

    By Nab

    Author Comment

    that helps.  I now get the alert.  However the cursor advances to the next text box.  Please advise.
    LVL 6

    Expert Comment


    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


    Author Comment

    onkeyup did not work, but advanced the cursor

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now