The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> JavaScript onBlur event to maintain focus
digitalpurpose
post May 20 2011, 03:38 PM
Post #1


Novice
**

Group: Members
Posts: 27
Joined: 7-March 11
Member No.: 14,063



Hello Everyone,

I'm trying to add some eMail validation to a form.

Basically, I am using an onBlur event on the email address field to check that the email is valid, and if not, to show an alert, empty the field, and restore focus to the email address field.

I am running into a problem, which is that when I move focus away from the email address field when an invalid address is entered, the alert is shown, the text is removed, but the focus is moved to whichever element I was moving to which caused the onBlur event.

I am thinking that I cannot set the focus to a field using an onBlur event from that field, but perhaps I am wrong.

The form's name is download-form, and the name and ID of the email address field is Eml. I am using the event onBlur="validateEmail();" on the email address input field, the code of validateEmail() is below.

Any help would be appreciated!

CODE

<script>
function validateEmail()
{
var x=document.forms["download-form"]["Eml"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Please Enter a Valid eMail Address");
  document.forms["download-form"]["Eml"].value = "";
  document.getElementById("Eml").focus();
  }
}
</script>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic
digitalpurpose   JavaScript onBlur event to maintain focus   May 20 2011, 03:38 PM
Darin McGrew   I've encountered forms that kept trying to mov...   May 20 2011, 05:38 PM
Christian J   but the focus is moved to whichever element I was...   May 20 2011, 06:32 PM
digitalpurpose   Currently, the focus is not being set to the email...   May 21 2011, 01:00 AM
Brian Chandler   Perhaps you are misunderstanding. Normally the a...   May 21 2011, 01:43 AM
digitalpurpose   Perhaps you are misunderstanding. Normally the ...   May 21 2011, 04:40 AM
Christian J   The current script I created does everything corr...   May 21 2011, 07:29 AM
Brian Chandler   Incidentally, a *sensible* form of onblur() feedba...   May 21 2011, 01:47 AM
Brian Chandler   Forcing the focus to where you think it ought to...   May 21 2011, 05:31 AM
Brian Chandler   Oh crumbs. So if I mistype the '@' as ...   May 21 2011, 05:33 AM
Darin McGrew   That would be nice. All too frequently, the normal...   May 21 2011, 10:12 AM
Brian Chandler   Realistically, no-one does this. They find a reg...   May 21 2011, 10:25 AM
digitalpurpose   Thanks for all of the replies. Christian J, your ...   May 21 2011, 11:30 AM
Christian J   Christian J, your solution is similar to what I w...   May 21 2011, 12:47 PM
Frederiek   Take a look here: http://www.xs4all.nl/~sbpoley/we...   May 22 2011, 04:09 AM
Christian J   I should add that in my onclick solution above, no...   May 23 2011, 06:46 PM
ChintooKhaade   Hi Everyone, In this demonstration we learn how to...   Sep 20 2011, 10:07 AM
chienline   Hello Everyone, I'm trying to add some eMail...   Oct 22 2012, 04:57 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 08:11 PM