javascript - validate HTML form input - prevent tab if invalid -


my html form includes series of 5 related <input type="text" /> tags, of need single-digit or two-digit numbers. i've created javascript function test user-input against regular expression using if-else statement. function works desired when input valid; however, have focus remain on input-field if input invalid, , far haven't been able make happen.

i've tried using document.getelementbyid(currseg).focus(); -- currseg dynamically set id of offending input-field. i've tried using switch statement individual case each possible bad input-field, , id of offending field specified directly rather calculated variable.

validation testing prompted onblur event (i've tried using onchange event) each input-field. have alert("bad user input") in else condition, know function flowing else condition when input invalid, focus moves next input-field.

everything i've tried fails stop progress of focus, whether move input-field mouse-action or tab-key.

any suggestions how can keep focus on field invalid input?

// without code example try use in function checking input - if not valid, focus element, return stops there... ( assuming context of function input element of course... )

this.focus();  return false; 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -