473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help Needed on setFocus (Doesnt work in Firefox)

Hi,

I have the following Javascript Code which checks if the user has entered numerics in the fields. If non numeric characters are found, it displays error message and then sets the focus back to the field. This works fine in Internet Explorer (v7) but it does not work in Firefox (v 2.0.0.1). The error message works but then the focus goes to the next field. Any help will be greatly appreciated. Thanks.

Expand|Select|Wrap|Line Numbers
  1. <!-- Begin
  2. function validate(field) {
  3.    var valid = "0123456789"
  4.    var ok = "yes";
  5.    var temp;
  6.    for (var i=0; i<field.value.length; i++) {
  7.         temp = "" + field.value.substring(i, i+1);
  8.         if (valid.indexOf(temp) == "-1") ok = "no";
  9.   }//end for
  10.   if (ok == "no") {
  11.         alert("Invalid entry! Only numbers are accepted!");
  12.         field.focus();
  13.         field.select();
  14.   }//end if
  15. } //end function
  16. //  End -->
  17.  
  18.  
Feb 20 '07 #1
6 2171
Logician
210 100+
Hi,

I have the following Javascript Code which checks if the user has entered numerics in the fields. If non numeric characters are found, it displays error message and then sets the focus back to the field. This works fine in Internet Explorer (v7) but it does not work in Firefox (v 2.0.0.1). The error message works but then the focus goes to the next field.
Alerting on blur is not recommended, as in operation it can cause endless loops.
Consider something less obtrusive, like changing the className of the field to show a red background, or making a red triangle visible adjacent to the field.
Feb 21 '07 #2
acoder
16,027 Expert Mod 8TB
Alerting on blur is not recommended, as in operation it can cause endless loops.
Consider something less obtrusive, like changing the className of the field to show a red background, or making a red triangle visible adjacent to the field.
Yes, that's right, see this link.
Feb 21 '07 #3
Thanks for your reply. I have checked the link to the web page suggested and it works fine on submit but it does not seem to work when the focus changes from field to field. The forms on which I am going to use is really very long and ideally the validation should happen as the user goes along the form. I have tried both Internet Explorer and Firefox and it does not work in both of use as you tab through the forms.

Thanks once again.
Feb 21 '07 #4
acoder
16,027 Expert Mod 8TB
Thanks for your reply. I have checked the link to the web page suggested and it works fine on submit but it does not seem to work when the focus changes from field to field. The forms on which I am going to use is really very long and ideally the validation should happen as the user goes along the form. I have tried both Internet Explorer and Firefox and it does not work in both of use as you tab through the forms.

Thanks once again.
The page works fine in Firefox. It displays the error message and gives focus back to the field with the error. Also, consider using regular expressions for your validation - it is better than the method you are currently using.
Feb 21 '07 #5
The page works fine in Firefox. It displays the error message and gives focus back to the field with the error. Also, consider using regular expressions for your validation - it is better than the method you are currently using.
Thanks for your response. I checked this once again. http://www.xs4all.nl/~sbpoley/webmatters/formval.html

It does not work in Firefox until you have pressed submit. What I am trying to achieve is that when users go through the form (using tab or click) if the fields are blank or have no numeric characters it should display error message and prompt the users back to the field. I like the way the error messages are displayed on the form rather than pop up alert. But it definitely does not work on the above page before submit is clicked.

Thanks once again and apologies if I am not being very clear.
Feb 21 '07 #6
acoder
16,027 Expert Mod 8TB
Ok, I see what your problem is. I only figured out what you wanted by pressing Send on that form. I think it should be easy to achieve what you want, but don't use onblur. Use onchange instead. What you could have is that what is achieved by the onsubmit function is called whenever a value is changed with one exception (don't validate fields that the user hasn't reached yet). Set a variable for each field that has been 'visited' by the user. Only those are validated whenever a field is changed.
Feb 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randi | last post by:
Hi All, I have a problem on a username password login form. I use: txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the proper case when someone enters their name and...
22
by: necromonger | last post by:
Hi, I've got this code that creates a new new row and cell. I then put some text into the cell with innerHTML - works beautifully with Firefox but fails with IE. I guess IE doesn't support this...
0
by: Paul T. Rong | last post by:
In form ¡°Order¡± there are 4 important controls: - combo1 (unbounded, source from ProductID field of Product table), - txt1 (I wish the product name will automatic show here after the user inputs...
2
by: Steve K | last post by:
I got a bit of a problem I like some help on. I'm designing an online training module for people that work in food processing plants. This is my target audience. These workers have little or no...
3
by: broli85 | last post by:
I am writing an extension for Firefox 1.5. I would like to know if there is a way to write and read data to and from a file which is in the location of the extension (or somewhere in the user's...
0
by: HaggardSmurf | last post by:
Alright well I had a question before when i was a noob and posted here and got such a great response this has become my main vb site. Anyways I have a comp that cant run msn due to it being a piece...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: ShutterMan | last post by:
I have a function that creates input buttons dynamically... function $NEW_BUTTON(id, content, parentId, cssclass, onclick) { var ctrl = document.createElement("input"); ctrl.id = id;...
20
by: Hush | last post by:
Hi, The following code works fine in IE7 but FF returns with an error: Access denied to achieve the property Element.firstChild. In this line: nodes = xmlDoc.documentElement.childNodes; My...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.