Solved

javascript to validate textbox onblur fails on the last textbox

Posted on 2006-04-22
6
1,126 Views
Last Modified: 2012-06-27
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
Comment
Question by:astar666
  • 2
  • 2
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
<FONT method post???
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
"Please advise" does not explain what you want, or need the script to do.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:astar666
Comment Utility
okay, I want the cursor to remain in the last text box, if the last text box is blank
0
 

Author Comment

by:astar666
Comment Utility
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
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now