473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Form validation help

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

Feb 25 '06 #1
2 1600
VK
> i = document.myForm.field1.value

This is a call for troubles, especially in IE. Better use the
conventional form addressing syntax. And always use "var" in variable
declaration. First you're avoiding global variable while you need a
local one. Secondly add <div id="i"></div> into your page for a sample
of other bad things it may lead to.

var v = document.forms['myForm'].elements['field1'].value;
if ( i < 10 or i > 20 ) {
There is not "or" operator in JavaScript. It's called "||" here.

if (v<10 || v>20) {

All together:

function validatefield1() {
var fld = document.forms['myForm'].elements['field1'];
var v = fld.value;
if (v<10 || v>20) {
alert('wrong input');
fld.select();
fld.focus();
}
}

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?


I donno - I guess the main part of errors came from "or". Overall you
have no way to guess why did user came to some field and why left it
unfilled for another field. Maybe she's using tab to navigate and she
wants to fill some underlaying fields first. Maybe she got blink in her
AIM so she needs to switch the window. Maybe she started to fill her
account info and she needs to look at another file for the last number.
It is futile tu guess and it is *highly* irritating to users to be
alert(ed) on each onblur.

I would suggest to drop this approach all together. You better put some
visual marks (say green) near each properly filled field as long as
your user navigates from field to field. And onsubmit you do one more
final check and place say red marks near all unfilled or unproperly
filled fields. Your users will just love this and its author :-)

Feb 25 '06 #2
On 25/02/2006 23:47, VK wrote:
i = document.myForm.field1.value
[...] And always use "var" in variable declaration.


The OP did. In both functions, it was the line prior to the one you
quote above.

[snip]
var v = document.forms['myForm'].elements['field1'].value;


When property names conform to the Identifier production, dot notation
will suffice:

var v = document.forms.myForm.elements.field1.value;

Better yet, avoid resolution of the control altogether:

<input name="field1" size="2" onchange="validateField1(this);">

function validateField1(control) {
var v = +control.value;

if ((v < 10) || (v > 20)) {
alert('Please enter a number between 10 and 20.');
if (control.focus)
control.focus();
return false;
}
return true;
}

By the way, it goes without saying that names like 'field1' are terrible.

[snip]
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.
Quite.
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.
Which is why it's imperative to avoid the blur event when messing with
control focus. In fact, the blur event should be avoided in general for
validation purposes as warnings can become, from the user's perspective,
harassment.
I've tried other combinations of events, such as ondeactivate, but
they all result in the same behavior.
Use the change event, as illustrated above. To catch omitted fields,
also validate using the submit event.
Why does the focus shift from field1 to field2 prior to the onblur
event completing?


It doesn't. The listener for the first control does complete, but the
loss of focus during the execution of that listener queues another blur
event. When the function returns, the event is dispatched into the
document tree, calling the other listener.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
14
by: JNariss | last post by:
Hello, I am fairly new to asp and jscript and am now in the process of learning some form validation. I am taking one step at a time by validating each field and testing it before moving onto...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
0
by: karen987 | last post by:
This is an email form on an ASP page. I want to add validation before it submits, The current validation only checks "name, email, and content" (server side) if the spaces are empty. I need to add...
3
uranuskid
by: uranuskid | last post by:
Hey folks, I was going to include a contact form on my website. Well, in the first place that seemed an easy thing to do with a form that prompts a PHP file validating the input vaiables and using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.