V
Vinny
A customer wants me to convert a VB app I wrote for them into a web app
for remote users to do data entry.
So, I'm writing a small ASP program which prompts the user for data,
much like a VB program. I would like to interactively validate the data
as they are entering it, rather than waiting until they submit the
form. So, I am doing something like this (this is a very simple
example):
<head>
<script language=javascript>
function validatefield1()
var i;
i = document.myForm.field1.value
if ( i < 10 or i > 20 ) {
alert("please enter a # between 10 and 20!");
document.getElementByID('field1').focus();
return false;
}
return true;
}
function validatefield2()
var i;
i = document.myForm.field2.value
if ( i < 20 or i > 30 ) {
alert("please enter a # between 20 and 30!");
document.getElementByID('field2').focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name=myForm id=myForm method=post action='/nextpage.asp'>
Field1: <input type=text size=2 id=field1 name=field1
onblur='validatefield1();'>
Field2: <input type=text size=2 id=field1 name=field1
onblur='validatefield2();'>
</form>
</body>
Here is the problem:
By default the first field has focus. If I press tab to bypass it
without entering anything, focus shifts to field2 immediately. The
onblur event fires, which checks the value in field1, sees it is
invalid, so prints an error message and resets the focus to field2.
Naturally, this causes field2 to lose focus. This causes field2's blur
event to kick of, which causes an error (since the field is blank),
printing an error, setting focus back to field2.
The effect is an endless loop of error messages.
I've tried other combinations of events, such as ondeactivate, but they
all result in the same behavior.
Why does the focus shift from field1 to field2 prior to the onblur
event completing?
Can someone explain the proper sequence of event processing on ASP
forms? When I press tab in field1, why does focus shift to field2 prior
to the onblur event for field1 completing? I can't seem to find this
documented anywhere in the msdn documentation to explain how events are
sequenced on IIS forms.
Alternatively, can someone suggest which event I would use to
accomplish what I'm trying to do? I've read the newsgroup and people's
comments about not using the onblur event, but I'd prefer to
interactively validate data, rather than waiting until the form is
submitted which I think is messy and unprofessional, compared to the VB
apps I've written. Plus the customer wants me to keep the web page
operationally identical to the VB app, and the VB app validates data at
the point of entry.
Thanks
Vin
for remote users to do data entry.
So, I'm writing a small ASP program which prompts the user for data,
much like a VB program. I would like to interactively validate the data
as they are entering it, rather than waiting until they submit the
form. So, I am doing something like this (this is a very simple
example):
<head>
<script language=javascript>
function validatefield1()
var i;
i = document.myForm.field1.value
if ( i < 10 or i > 20 ) {
alert("please enter a # between 10 and 20!");
document.getElementByID('field1').focus();
return false;
}
return true;
}
function validatefield2()
var i;
i = document.myForm.field2.value
if ( i < 20 or i > 30 ) {
alert("please enter a # between 20 and 30!");
document.getElementByID('field2').focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name=myForm id=myForm method=post action='/nextpage.asp'>
Field1: <input type=text size=2 id=field1 name=field1
onblur='validatefield1();'>
Field2: <input type=text size=2 id=field1 name=field1
onblur='validatefield2();'>
</form>
</body>
Here is the problem:
By default the first field has focus. If I press tab to bypass it
without entering anything, focus shifts to field2 immediately. The
onblur event fires, which checks the value in field1, sees it is
invalid, so prints an error message and resets the focus to field2.
Naturally, this causes field2 to lose focus. This causes field2's blur
event to kick of, which causes an error (since the field is blank),
printing an error, setting focus back to field2.
The effect is an endless loop of error messages.
I've tried other combinations of events, such as ondeactivate, but they
all result in the same behavior.
Why does the focus shift from field1 to field2 prior to the onblur
event completing?
Can someone explain the proper sequence of event processing on ASP
forms? When I press tab in field1, why does focus shift to field2 prior
to the onblur event for field1 completing? I can't seem to find this
documented anywhere in the msdn documentation to explain how events are
sequenced on IIS forms.
Alternatively, can someone suggest which event I would use to
accomplish what I'm trying to do? I've read the newsgroup and people's
comments about not using the onblur event, but I'd prefer to
interactively validate data, rather than waiting until the form is
submitted which I think is messy and unprofessional, compared to the VB
apps I've written. Plus the customer wants me to keep the web page
operationally identical to the VB app, and the VB app validates data at
the point of entry.
Thanks
Vin