By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,835 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

Help Needed on setFocus (Doesnt work in Firefox)

P: 3
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
Share this Question
Share on Google+
6 Replies

100+
P: 210
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
Expert Mod 15k+
P: 16,027
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

P: 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
Expert Mod 15k+
P: 16,027
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

P: 3
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
Expert Mod 15k+
P: 16,027
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

Post your reply

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