473,396 Members | 2,076 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,396 software developers and data experts.

onBlur madness!

Hello,

I'm having trouble with a bit of code that is both validating and
updating fields on the fly. Here's something similar to my code:
-----------
<script>
function validate(field) {
var frm = document.theForm;

if (field.value != "") {
frm.field3.value = Number(frm.field1.value) +
Number(frm.field2.value);
} else {
alert("Field is empty.");
}
}
</script>

<body>
<form name="theForm">
Field 1 <input name="field1" type="text" onblur="return
validate(this)">
<br><br>
Field 2 <input name="field2" type="text" onblur="return
validate(this)">

<br><br><br>
Total <input name="field3" type="text">
<br><br>
<input type="button" value="Validate">&nbsp;<input type="button"
value="Cancel" onClick="window.location.href='http://www.google.com';">

-----------

So, I need to

1. Have the "Total" field update whenever a user changes a value in
field 1 or field 2.
2. Perform some validation on those fields
3. Have the onblur and validation NOT fire when the user clicks the
Cancel button.

Problem: click in the first field, don't enter a value, then try to
press Cancel - the validation is happening, and I don't want it to.

This third point above seems to be the kicker. Anyone know a way I
can go from a text field to clicking the "Cancel" button without
firing the onBlur event? I normally don't like doing validating
on-the-fly like this, but my application has the requirement of
updating that Total box dynamically, so I have to perform field-level
validation as I go. Thoughts?

Thanks!
Jul 23 '05 #1
2 2415
Lee
Eric said:
1. Have the "Total" field update whenever a user changes a value in
field 1 or field 2.
2. Perform some validation on those fields
3. Have the onblur and validation NOT fire when the user clicks the
Cancel button.


One way to implement precognition is to delay the validation
for about a tenth of a second via setTimeout(), and have the
Cancel button clear the timer so the validation never happens.

Validating onBlur is almost always a bad idea, because there
are so many things that can cause a field to lose focus.
Popping up an alert(), for example. It's better to use
onChange, if at all possible.

You're going to want to sum your fields again on the server
side, because it's too easy for users to bypass your code
and submit bogus numbers.

Jul 23 '05 #2
On 17 Sep 2004 15:04:46 -0700, Eric <er*********@hotmail.com> wrote:

[snip]
<script>
SCRIPT elements require a type attribute:

<script type="text/javascript">
function validate(field) {
var frm = document.theForm;
All form controls have a property, form. This provides a reference to the
containing form, making the name attribute unnecessary.

var frm = field.form;
if (field.value != "") {
frm.field3.value = Number(frm.field1.value) +
Number(frm.field2.value);
If you're going to validate values, actually check that the user entered
numbers before trying to perform any arithmetic.
} else {
alert("Field is empty.");
}
}
</script>

<body>
<form name="theForm">
As I said above, the name attribute is unnecessary if you use the form
property.
Field 1 <input name="field1" type="text" onblur="return
validate(this)">
Don't use the blur event for field validation. Use change.

[snip]
1. Have the "Total" field update whenever a user changes a value in
field 1 or field 2.
2. Perform some validation on those fields
3. Have the onblur and validation NOT fire when the user clicks the
Cancel button.


The first two are easy, but the third is all but impossible by
conventional means. The field-fired events, whether you use blur or
change, will occur before the click event is fired. About the only
work-around is to delay the validation, giving the click event time to
occur and cancel it. However, that's a horrible solution and potentially
unreliable.

One possibility is to update the defaultValue property and make the Cancel
button a reset type, or something similar. Storing the value in a custom
property, perhaps. When the button is clicked, the stored values can
replaced those that have been changed, but it leaves one crucial issue:
how do you determine what constitutes a committed value with out expressly
requiring user action? A Calculate button, for example.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

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

Similar topics

1
by: Barry Svee | last post by:
I'm struggling with the JavaScript blur event. I have an activex control that I need to interact with when a window loses focus, namely to instruct it to give up control of a barcode scanner so...
1
by: Tzachi | last post by:
Hello all, I have a function that dynamically adds rows and columns to the page. Everything works well except onfocus // onblur attributes. For some reason, when entering the input box it...
2
by: D. Alvarado | last post by:
Hi, I'm having some trouble with the "onBlur" event in the BODY tag. Ideally, what I want to happen is that when someone leaves window A, window A executes a command. I had put <body...
6
by: rob | last post by:
Hi I'm trying to create a "roll-up" effect when a window loses focus and then "roll-down" when it regains focus. This statement works properly with every browser I can get my hands on EXCEPT...
5
by: Dave Hammond | last post by:
Hi All, I have a web form which performs certain actions upon moving focus away from a field. However, if the user clicks the top corner 'X' icon to close the window, the onBlur event still...
3
by: Robert Oschler | last post by:
I have a textarea element that I have created an onblur() handler for. In the onblur() handler, I check to make sure that they have saved the contents of the edit box, before leaving it. If...
1
by: neil S via DotNetMonster.com | last post by:
I have a custom control with a textbox and dropdown list. The dropdown list is hidden and acts as a data source for the textbox. When the user enters text in the textbox, an onKeyup event is...
1
by: Duncan | last post by:
Hi all, I'm tearing my hair out on this! I have a simple ASP.NET 2 page with a TextBox control, in the PreRender I set ClientSide events:- Response.Attributes.Add("onchange",...
4
by: rubenhan | last post by:
Hey, guys. My basic idea is this. When I write "1" in the pre-existing text field and onblur, I want to add a new field with a set of attributes, which turned out to be the same as the pre...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.