Help w/ interactive form data validation

V

Vinny

I'm writing a small ASP program which prompts the user for data. 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'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.

Thanks
Vin
 
J

Jeff Cochran

I'm writing a small ASP program which prompts the user for data. I
would like to interactively validate the data as they are entering it,
rather than waiting until they submit the form.

Can't in ASP. Client side code would be needed, until the form is
submitted the server can't respond.

Jeff
 
P

Paxton

Vinny said:
I'm writing a small ASP program which prompts the user for data. 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?

ASP is irrelevant in your context, and there is no such thing as an
"ASP" form. There is an HTML form, which you can use server-side ASP
to process, or you can use client-side javascript (or a mixture of
both).

In your case, you would be better off asking this question in a
client-side code newsgroup.

/P.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top