/    Sign up×
Community /Pin to ProfileBookmark

dynamic focus/blur alert problem

I have a form that gets built dynamically with php and mysql.

I have JS functions that color the field when onfocus then back to normal onblur. This has always worked. Now I am dealing with a field that if a unit (from a dealership) is a licensed vehicle I have to force them to enter in 17 digit VIN.

What I am trying to do is if they enter one of these fields, and try to leave without typing anything (I do not want them stuck there with alerts never being able to leave) it is fine it just colors it while they are in and onblur normal. But if they are in one of these fields and it is a forceVIN unit and they type less than 17 digits, I want to alert them then place the focus back inot that same field.

Here is what I have, right now the alert works in mozilla and places the focus in another field with both being colored. IE once you try to onblur you have a alert loop you can not get out of unless you shut down IE.

form name is receive. Thanks for any help

[code]
<SCRIPT LANGUAGE=”JavaScript”>
<!–this is for our onfocus field colors–>
function chgit(chgit)
{
chgit.style.backgroundImage=”url(../images/box2_bg.gif)”;
}

// old color
function chgback(chgit)
{
chgit.style.backgroundImage=”url(../images/box_bg.gif)”;
}

// old color and handles our force 17 digit vin as well
function chgback2(chgit)
{
if (chgit.value.length !=”17″)
{
alert (“This unit is a licensed vehicle and must a 17 digit VIN”);
chgit.focus();

return false;
}

chgback(chgit);
}

</script>
[/code]

[code]
//here we check to see if we are forcing a 17 digit VIN
if ($row[“ForceVIN”] == 1)
$value .= “<input type=text name=serial”.$j.” size=20 maxlength=20 value=”” onFocus=”chgit(this)” onBlur=”chgback2(this)”>”;

else
$value .= “<input type=text name=serial”.$j.” size=20 maxlength=20 value=”” onFocus=”chgit(this)” onBlur=”chgback(this)”>”;

[/code]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@CharlesOct 27.2004 — Validate "onchange".
Copy linkTweet thisAlerts:
@tripwaterauthorOct 27.2004 — Thanks for the reply but I am not real good with JS could please explain further?

THanks again
Copy linkTweet thisAlerts:
@tripwaterauthorOct 27.2004 — ok I think I understand.

I set the original chgit(chgit) & chgback(chgit) to onfocus and onBlur and set my


function chgback2(chgit)

if (chgit.value.length > 0 && chgit.value.length !="17")

{

alert ("This unit is a licensed vehicle and must a 17 digit VIN"); chgit.focus();

return false;

}

}


Then call onChange=chgback2(this)

That works and gets me out of hte alert problem but now how can I get the focus back to the field that just alerted?

Right now the focus goes to the next field in line (next row)

Thanks again. I am almost where I need to be.?
Copy linkTweet thisAlerts:
@JPnycOct 27.2004 — I don't understand the problem here. If the length of the field is 17, then the alert should not fire at all. If it does, something else is wrong. By the way, 17 shouldn't have "" around it. It's an int not a string. As soon as you put "" around it, it becomes string.
Copy linkTweet thisAlerts:
@tripwaterauthorOct 27.2004 — Ok I will remove the quotes. No, my alert fires if less than 17. That works, only it places the focus in the next field rather than the one that prompted the alert.

Thanks again
Copy linkTweet thisAlerts:
@JPnycOct 27.2004 — Oh, well just fix the focus problem then. Use either the name or the id of that field, and add .focus(). What's the problem? Just make sure it doesn't fire unless the alert does.
Copy linkTweet thisAlerts:
@tripwaterauthorOct 27.2004 — look at my function please...I copied and pasted the code and the place focus is at the end of a line....sorry formatting


But I am trying to place focus. Look at it and tell how that is incorrect please.
Copy linkTweet thisAlerts:
@JPnycOct 27.2004 — THat's not enough to reference an element inside a form. you need

document.forms[0].chgit.focus();
×

Success!

Help @tripwater 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,
)...