• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1135
  • 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
 
HonorGodCommented:
"Please advise" does not explain what you want, or need the script to do.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
HonorGodCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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