?
Solved

javascript to validate textbox onblur fails on the last textbox

Posted on 2006-04-22
6
Medium Priority
?
1,132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16517127
<FONT method post???
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16517134
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
ID: 16518896
"Please advise" does not explain what you want, or need the script to do.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

Author Comment

by:astar666
ID: 16519448
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 1500 total points
ID: 16520676
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…
Suggested Courses

764 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