• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1137
  • Last Modified:

javascript to validate textbox onblur fails on the last textbox

I have the following javascript:

      

<script>
var focusField;
function MustEnter(oTextbox) {
  if(focusField && focusField!=oTextbox) return false;
     if (oTextbox.value.length==0) {
          alert("Must enter");
          oTextbox.select();
          focusField = oTextbox;
          setTimeout("focusField.focus();focusField=null;",1);
          return;
     }
     
}

</script>

I have the following html:


<font action="/cgi-bin/registration.py" method="post">
<table>
<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>
</table>
<input type="submit" value="Submit">
</form><br>

The javascript puts up an alert and keeps the focus on the textbox if I try to enter blanks.  This is so up until the email field which is the last textbox on the page.  There focus moves freely and there is no alert.  Please advise.
0
astar666
Asked:
astar666
  • 2
  • 2
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
<FONT method post???
0
 
ZvonkoSystems architectCommented:
And my function works as expected. I do not see any errors.
Of course the checking could be extended not to check only length but to check for any non blank value or event correct email address format, but you did not say the magic words in my last support to you ;-)
0
 
HonorGodSoftware EngineerCommented:
"Please advise" does not explain what you want, or need the script to do.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
astar666Author Commented:
okay, I want the cursor to remain in the last text box, if the last text box is blank
0
 
astar666Author Commented:
you can see the error in action at www.timedimension.com.  Using mozilla, chose the login which has on the login page an option to register.  Chose that and fill in all the items except email.  Try to tab off the blank email and you succeed and it is desired that you get the alert and the focus stays on the email textbox.
0
 
HonorGodSoftware EngineerCommented:
hm... It might be Mozilla specific.  I get that action while using FireFox.  If I try using tab, or shift-tab from the e-mail field, the obnoxious alert box keeps displaying, and the focus is put on the e-mail address field.  However, if anything (even a blank) is entered, the you can tab from this field.

I think that it would be better if you disabled the Submit button until all (required) fields are valid.  But that is just a suggestion.

hm.  I tried using:
- FireFox 1.5.0.2
- Internet Explorer 6.0.2900
- Mozilla 1.7.8
- Netscape 8.0.4
- Opera 8.54

  all of which performed as you describe above.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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