/    Sign up×
Community /Pin to ProfileBookmark

Focus using a variable

I have a form with 42 text cells that are all being validated by the same script. If the proper time format isn’t entered, it properly detects it and says so. Each cell is passing its id to the script.

Where I’m running into trouble is in trying to put focus back on the offending cell when the data is not formatted properly.

How do I properly format a variable name in:
document.formname.variable.focus()

function timecheck(id) {
var pattern = “^((d)|(0d)|(1d))?(d)|([0-5]d)) (AM|am|PM|pm)$”
var input = document.getElementById(id).value;

if (input.match(new RegExp(pattern)))
{
}
else
{
window.alert(“Please use a proper time format such as 9:15 AM or 12:30 PM”);
//need to focus here
}

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mrhooApr 10.2008 — You need a short timeout after the alert-

var who=document.getElementById(id);

window.alert("Please use a proper time format such as 9:15 AM or 12:30 PM");

setTimeout(function(){who.focus()},100);

You may find it friendlier to use a prompt instead, and fill the value with a valid response from the prompt-
[CODE]else{
while(!input.match(new RegExp(pattern)){
input= prompt('use format 9:15 AM or 12:30PM','');
}
document.getElementById(id).value= input;
}[/CODE]
Copy linkTweet thisAlerts:
@SaugatuckeeauthorApr 10.2008 — Thanks the timeout worked.

Your suggestion for prompts is well taken, but the user may only be making an occasional entry in any one of the 42 cells. It's easy enough to just let them click and type - which is now functioning as desired.
×

Success!

Help @Saugatuckee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...