focus problems

J

Jens Körte

Hi NG!

I encountered some strange behaviour when using focus()

I use a form with several input-fields. A user can enter stuff. After
entering I want to check the value, i.e. that a number is only a
number etc.
I therefore started editing a Javascript. For test purposes it only
popup a message and sets the focus. In fact this is what I want to do,
but the scipt isn't it doing right.

Here is the script:

<script language=JavaScript>
function check(wert)
{
switch (wert)
{
case "Firma":
if (document.Anmeldungsform.Firma.value == '')
{
alert("Bitte geben Sie Ihre Firma ein...");
document.Anmeldungsform.Firma.focus();
}
break;
case "Vorname":
if (document.Anmeldungsform.Vorname.value == '')
{
alert("Bitte geben Sie Ihren Vornamen ein...");
document.Anmeldungsform.Vorname.focus();
}
break;
case "Nachname":
if (document.Anmeldungsform.Nachname.value == '')
{
alert("Bitte geben Sie Ihren Nachnamen ein...");
document.Anmeldungsform.Nachname.focus();
}
break;
}
}
</script>

And now the HTML-Part
<form name="Anmeldungsform" method="POST" action="mailer.php">
<p>Firma:
<input type="text" name="Firma" onChange="check('Firma')">
<br>
Ansprechpartner:<br>
Vorname:
<input type="text" name="Vorname" onChange="check('Vorname')">
<br>
Nachname:
<input type="text" name="Nachname" onChange="check('Nachname')">
<br>
</form>


Ths thing that happen is, that after entering noting in field 1
nothing happens. Focus changes to field 2 and no popup is shown. This
happens to all of the fields.
When I enter a value and then deletes the value I encounter no changes
of the shown behaviour.
Only if I enter a value, move to the next field, go back and delete
the value and the force to go to the nect field I get a popup. After
closing the popup the focus remains on the next field, i.e. it doesn't
jump back to the field where the user have to make changes.
I tried this script mainly on Mozilla, but also on IE.
Please help me

Jens Körte
 
M

Michael Winter


Hello. :)
I encountered some strange behaviour when using focus()

The focus method isn't the problem.

[snip]
<script language=JavaScript>

(This won't affect the outcome, by the way.)

The language attribute is deprecated and has been for about six years! Use
the (required) type attribute, instead:

<script type="text/javascript">

[snip]
When I enter a value and then deletes the value I encounter no changes
of the shown behaviour.
Only if I enter a value, move to the next field, go back and delete the
value and the force to go to the nect field I get a popup.

In the first case, the value is edited, yes, but it hasn't actually
changed. The value of the control when focus was gained was the same as
when focus was lost. That is how "change" is determined, and why
validation should occur (primarily) when the form is submitted (both
server- and client-side).

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top