Results 1 to 2 of 2

Thread: select() in a form - javascript

  1. #1
    Join Date
    Aug 2007
    Location
    Barcelona
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question select() in a form - javascript

    Hi. I have written the following text which works totally except for one thing. If the email input box has been typed into, after the message "Please enter a valid email address"l, I want that text written in error to be highlighted so the client doesn´t have to delete it and can just type straight back in with a corrected email address.

    function email_onblur()
    {
    var email = document.form1.email.value;
    var atSign = email.indexOf("@")
    if ((atSign == -1) == true)
    {
    alert("Please enter a valid email address.");
    myForm.email.focus();
    myForm.email.select();

    }
    }

    I feel it is the text highlighted in green above that is not working, everything else is ok. Any ideas?

    Many thanks.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If:

    Code:
    myForm.email
    refers to the proper element, then after focusing on it:

    Code:
    myForm.email.select()
    will select it. However, you may have the wrong element, or no element.

    In an effort to eliminate those possibilities, try:

    Code:
    function email_onblur(){
    var e = document.form1.email;
    var atSign = e.value.indexOf("@")
    if (atSign < 0){
    alert("Please enter a valid email address.");
    e.focus();
    e.select();
    }
    }
    Also, the use of an alert my be overriding the focus() and/or select() directives - or just be changing the focus and selection of the document after those directives. Remember, you have to click on an alert to close it.

    If you need more help:

    Please post a link to the page on your site that contains the problematic script so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •