Solved

javascript to validate textbox onblur fails on the last textbox

Posted on 2006-04-22
6
1,128 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

831 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