Trapping return for form field focus

Today I had to cook up a sim­ple form with two input fields, so that two bar­codes could be scanned into their respec­tive fields and then sub­mit­ted (the point being to link two IDs in a data­base that have been encoded in sep­a­rate bar­codes). There was one twist.

The bar­code reader auto­mat­i­cally appends a return char­ac­ter to the end of the string it’s read… which would, in any nor­mal cir­cum­stance, sub­mit the form. Obvi­ously prob­lem­atic unless we split the form over sev­eral pages, which is just yuck.

If the bar­code reader hadn’t returned char­ac­ter 13 (return/enter/whatever you’ll call it) at the end of the string, it’d be triv­ial to pick up a “maxlength=x then go to next field” script off the side of the road… they’re every­where. Not so much the case with this exact prob­lem, though, so I thought I’d share…

[source:str javascript]function catchEnter(e){
var char­ac­ter­Code
if(e && e.which){
e = e
char­ac­ter­Code = e.which
} else {
e = event
char­ac­ter­Code = e.keyCode
}
if(characterCode == 13){
document.getElementById(‘cardid’).focus();
return false
} else{
return true
}
}[/script]

Note we’re not using DOM meth­ods here… there isn’t any equiv­a­lent to which or keyCode that I’m aware of (I looked enough). keycode is the impor­tant one… which is used by the likes of Netscape 4.x and other nas­ties… I don’t really want to know about it, but I stole the key trap code from some­where (lost the site) and didn’t really have a rea­son to inten­tion­ally break the behav­iour for those browsers!

So we use one of those (prob­a­bly key­code) to set char­ac­ter­Code, which is a numeric value that cor­re­sponds to Uni­code dec­i­mal val­ues. 13 is car­riage return. Then it needs to be com­pared to event (the char­ac­ter that trig­gered the onkey­press event), here used as e for con­ve­nience… and if this is true, then focus will go to the next field (in this case cardid) and the char­ac­ter will return false to pre­vent the form from submitting.

Screenshot of the demo page

I’ve got a sta­tic demo here (don’t mind the mes­sage at the top, it was an HTML mockup)… try enter­ing some­thing into the first field and press­ing return. Then press return again, and the form will submit.

Obvi­ously this Javascript only works for a two-field case… but you could dynam­i­cally set the ele­ment for focus to fol­low to by pars­ing that through to the func­tion onkeypress event. The only other thing I can think of is to getEle­ments­By­Tag­Name every input field in the form and use the array to dynam­i­cally set the “next” field… but that would have been waaaay overkill for what I had to do.

This behav­iour isn’t just use­ful for bar­code scan­ners, by the way. Desk­top appli­ca­tions often exhibit this kind of behav­iour, and it also goes some way to ensur­ing all fields are filled with­out doing for­mal val­i­da­tion (either JavaScript on submit, or server-side).

Tags: , ,

posted on Tuesday, February 21st, 2006 at 7:44 pm by Josh Street, filed under Usability, Web Standards.

8 Responses to “Trapping return for form field focus”

  1. Matthom says:

    I’m not quite fol­low­ing — why would some­one hit “Return” after the first field, rather than “Tab?” Or is it the scan­ner that would be inputting the text into the fields?

  2. Josh says:

    Yes, the scan­ner is inputting text. It enters a string of num­bers, then a return char­ac­ter. This way, the oper­a­tor has to just go “click” on two dif­fer­ent bar­codes and not touch the key­board at all.

    My point at the end was that this extends to other appli­ca­tions where users expect to be able to press return to change field focus (instead of tab, yeah)… SOME desk­top appli­ca­tions have exhib­ited this behav­iour in the past (names/examples don’t spring to mind, sorry).

  3. Matthom says:

    We have a sim­i­lar sit­u­a­tion, here at work — where we use UPS World­Ship, and FedEx ship­ping soft­ware — each of which we can scan in ser­ial num­bers, bar­codes, etc. We can also scan them directly into OUR soft­ware inter­face, using a typ­i­cal text field. We don’t, how­ever, have “Return” auto­mat­i­cally “sub­mit the form,” even though this exam­ple proves we could. (For our app, the user still has to hit return, or click “submit.”)

  4. Josh says:

    Yeah… same kind of thing. We just had to build some­thing quickly and a web inter­face was the fastest way for us to do that, hence the need for weird char­ac­ter trap­ping stuff :P Also, there are about 80,000 bar­codes to be scanned twice (80,000 records to be linked to 80,000 cards), so the time taken to click “Sub­mit” adds up… lots.

  5. Sam says:

    Sigh, John Doe is at it again…
    I swear that guy must be bank­rupt by now. He is always post­ing his bank account and credit card num­bers all over the place.

  6. Marvin Hunkin says:

    Hi.
    now did an assign­ment for my web admin course, where i had to cre­ate a user login form and a script, which would use the user­name and pass­word fields, in a login form. and show a mes­sage that i logged in, and the cur­rent local time.
    now did that, but when i load the form, UserDetails.html, instead of going to the first form field, auto­mat­i­cally , it goes to the login but­ton. now is there a javascript func­tion, which will do this, and tried look­ing on the net, look­ing for a sim­ple javascript, to use the body onload func­tion, to load the func­tion when the form loads.
    now i knew how to do that a while back, but have for­got­ten, and tried sev­eral things, but still not work­ing.
    using tex­pad for my text edi­tor.
    now if any one has got any ideas, or sug­gest how to do this, Joe, had a look at your script, and way too com­plex for what i want it to do, all i want is the script to load the form, land on the first field, user­name, and then can fill in the details.
    which then loads a php script which i writ­ten and load easy php, what i am using for my php lan­guage.
    then use the http://localhost/UserDetails.html to load the form, fill in the details, click on login.
    wellah!
    there it is.
    the mes­sage, and the cur­rent time.
    so if any one can help, e-mail me.
    cheers Marvin.

  7. Josh says:

    Mar­vin:

    window.onload = function () {
    	document.getElementById('elementID').focus();
    	}

  8. David says:

    I can see this is very use­fulll, a lot of peo­ple is used to press “return/enter” to change from one cell to the nex when work­ing with excel (and other spread­sheets to) files to fill columns of a list, and press­ing tab changes cell in other direc­tion, so peo­ple often use “return/enter” to change field as well. I think that if the next field is to the right of the pre­vius field, peo­ple prob­a­bily press TAB, but if next field is below the pre­vius field, many peo­ple will press “return/enter”.

Leave a Reply