Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disable Enter on NS6+

    I use the following to trap the enter key when clearing an alert box:

    function handleEnter (field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13)
    {
    var i;
    for (i = 0; i < field.form.elements.length; i++)
    if (field == field.form.elements[ i])
    break;
    i = (i + 1) % field.form.elements.length;
    field.form.elements[ i].focus();
    return false;
    }
    else
    return true;
    }

    This work fine in IE6 and Opera (the latest) but not in NS6+. Web searching hasn't come up with much -- does anyone have any suggestions?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Disable Enter on NS6+

    I have no idea what this code does. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> But I do have two questions:

    First, does this line return the value you expect in Mozilla-based browsers?

    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

    Second, I'm not familiar with this syntax:

    field.form.elements.length

    Does that style of object reference actually work outside of the IE DOM? I tend to assign an ID, pass it as strFieldID and then use getElementByID (not sure that's the exact syntax, but no time to look it up) rather than relying on IE4-style passing of object references.

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disable Enter on NS6+

    Yes, the code is a bit strange. I took it from another site as a method for disabling the Enter key and it DOES work in IE 6+ and Opera.

    To explain a little more in detail:

    1. I have some validation stuff on a textbox. User plugs "something" into the box and tabs out
    2. If the "something" doesn't validate, an Alert is raised, the user clicks 'OK' or (if you're me) hits the enter button)
    3. The validation code returns focus to the textbox so you can change it
    4. If you hit Enter *again* (that is, don't bother correcting the problem), the form will proceed to submit. That where this code comes in: it disables Enter as a way to submit the form. You can comment out all the nonsense about field.form.elements[ i]; that isn't even correct syntatically....

    Well, actually, now that I look things over again with a fresher eye, I think I see the problem a little more clearly:

    On fields requiring validation, I disable the Enter key for submitting the form. I'd like to just do this for the entire form. I suppose it would be an onLoad() function like so:
    <font face="Georgia">
    function onLoad()
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13)
    {
    return false;
    }
    else
    return true;
    }
    </font face=georgia>

    So that, no matter WHERE you are or what you're doing, the Enter key won't trigger submit. Any hints on how to do that?

    AND, as to the other problem with Netscape not playing like IE and Opera, I think it's got to do with this part of the main validation code:
    <font face="Georgia">
    if (!result)
    {
    alert('Please enter a date in the format MM/DD/YYYY for the ' + fieldLabel + ' field.')
    formField.focus(); <-- the non-performer in Netscape!!
    return false;
    }
    </font face=georgia>
    I've tried several things:
    document.form.formField.focus() ;
    loading the function with a variable for the form name and trying again;
    loading the function with variables for the form name, the control name...

    in general, trying something like
    document.[form name].[form control name].focus() doesn't work in this code for NS and cause the code to not work in IE.

    Is it possible NS insists on literal DOM paths to a control? I know formField.focus() might not be enough for NS but when I try to be a little more literal IE and Opera go south and NS does not deign to acknowledge my petitions.

    I'm trying to get the validation and re-focus to fire with onBlur() rather than in a main validateForm(theForm) block so people won't get bugged after they have slogged thru a form.

    So, to sum up:
    1) What do you need to do to globally ban ENTER from causing the form to submit?
    2) How do you in code generically specify a formField focus() that works in across browsers?

Posting Permissions

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