473,413 Members | 1,757 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,413 software developers and data experts.

Checking for null value in a field

I've got Javascript in a form that adds up all of the numbers in a
column of form fields and displays a total. It works great if every
field has an initial value of 0, but if any of them are null, "NaN"
displays as the total. I tried to add a test for a null value with
the intent to skip adding that field to the accumulator for one
iteration of the "for" loop, but it doesn't work. Can anyone tell me
what I'm doing wrong? Below is the javascript:

function SumFundraisVol(){
var TotalSumFundRaisVol = 0;
var field = 0;
//Search through the entire array of form elements and
//locate all fields that include the word "Fundrais_vol"
//in their name. When found, add the value of that form
//element to TotalSumFundRaisVol
for (var i = 0; i<document.forms[0].elements.length; i++) {
field = document.forms[0].elements[i].name.indexOf('Fundrais_vol');
if (field > -1 && field != null) {
TotalSumFundRaisVol +=
eval(document.forms[0].elements[i].value);
}
}
document.forms[0].Fndraising_Vol_Total.value =
TotalSumFundRaisVol;
}
Jul 23 '05 #1
4 11748
essayez l'opιrateur typeof

Returns a string that identifies the data type of an expression.

typeof[(]expression[)] ;

Arguments

expression
Required. Any expression.
Remarks

The typeof operator returns type information as a string. There are six
possible values that typeof returns: "number", "string", "boolean",
"object", "function", and "undefined".

The parentheses are optional in the typeof syntax.
Note : All expressions in JScript .NET have a GetType method. This
method returns the data type (not a string representing the data type)
of the expression. The GetType method provides more information than the
typeof operator.

G Roydor

Tom Esker a ιcrit:
I've got Javascript in a form that adds up all of the numbers in a
column of form fields and displays a total. It works great if every
field has an initial value of 0, but if any of them are null, "NaN"
displays as the total. I tried to add a test for a null value with
the intent to skip adding that field to the accumulator for one
iteration of the "for" loop, but it doesn't work. Can anyone tell me
what I'm doing wrong? Below is the javascript:

function SumFundraisVol(){
var TotalSumFundRaisVol = 0;
var field = 0;
//Search through the entire array of form elements and
//locate all fields that include the word "Fundrais_vol"
//in their name. When found, add the value of that form
//element to TotalSumFundRaisVol
for (var i = 0; i<document.forms[0].elements.length; i++) {
field = document.forms[0].elements[i].name.indexOf('Fundrais_vol');
if (field > -1 && field != null) {
TotalSumFundRaisVol +=
eval(document.forms[0].elements[i].value);
}
}
document.forms[0].Fndraising_Vol_Total.value =
TotalSumFundRaisVol;
}


Jul 23 '05 #2
G Roydor wrote:
<snip>
typeof[(]expression[)] ; <snip> The parentheses are optional in the typeof syntax.

<snip>

Parentheses are not optional in the "typeof syntax" they are irrelevant
to it. typeof is a unary operator and acts on a UnaryExpression (ECMA
262 3rd ed. section 11.4.3). The Parentheses (in that context) are
Grouping Operators (ECMA 262 3rd ed. section 11.1.6) and surround an
expression, resulting in an expression. The use of Grouping Operators
around an expression that typeof operates on, or their omission, makes
no difference to the typeof operation as the expression is evaluated
first and typeof only acts on the result of that evaluation.

Richard.
Jul 23 '05 #3
On 4 Apr 2004 16:49:32 -0700, Tom Esker <tj*****@yahoo.com> wrote:
I've got Javascript in a form that adds up all of the numbers in a
column of form fields and displays a total. It works great if every
field has an initial value of 0, but if any of them are null, "NaN"
displays as the total. I tried to add a test for a null value with
the intent to skip adding that field to the accumulator for one
iteration of the "for" loop, but it doesn't work. Can anyone tell me
what I'm doing wrong? Below is the javascript:


There are some fundamental problems with this script.

First of all, if a text field is blank, it's value is not null, but an
empty string ('').

field = document.forms[0].elements[i].name.indexOf('Fundrais_vol');
if (field > -1 && field != null) {

Second, the variable, field, has nothing to do with the value of the
field. It contains the index of the string, 'Fundrais_vol'.

Thirdly, you don't validate the text fields to ensure that the value
represent numbers.

Finally, using eval() to convert a string to a number is a very poor way
to accomplish that goal.

Try:

// By convention, functions start with a lowercase letter
function sumFundraisVol()
{
var element, form = document.forms[ 0 ],
collection = f.elements, total = 0, value;

for( var i = 0, n = collection.length; i < n; ++i )
{
element = collection[ i ];
if( -1 != element.name.indexOf( 'Fundrais_vol' ))
{
// Attempt to convert the text value to a number
value = +element.value;
// Check if the text did represent a number
if( !isNaN( value )) {
total += value;
}
}
}
form.Fndraising_Vol_Total.value = total;
// Read <URL:http://jibbering.com/faq/#FAQ4_6> for information
// on formatting numbers as strings.
}

[snipped code]

I haven't tested the code above, so do check it thoroughly before using it.

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
JRS: In article <op**************@news-text.blueyonder.co.uk>, seen in
news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.invali
d> posted at Mon, 5 Apr 2004 23:27:43 :
// Attempt to convert the text value to a number
value = +element.value;
// Check if the text did represent a number
if( !isNaN( value )) {
total += value;


Valid, of course (FAQ 4.21 mentions that use of +); OTOH, if the OP is
checking input at all, ISTM wise to check it thoroughly.

It seems likely that value should be non-negative, and that there is an
upper limit of credibility, and that any letter, even e or O, in the
number must be an error.

if (/^(\d{1,6})$/.test(element.value)) total += +RegExp.$1
or
OK = /^(\d{1,6})$/.test(element.value)
if (OK) total += +RegExp.$1 ; else alert("Aaarrgh!")
or ...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

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

Similar topics

10
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: šššššššštypedefšstructš{ ššššššššššššššššintššššši; ššššššššššššššššcharššššname; šššššššš}šMY_TYPE; ššššššššconstšcharš*
2
by: Chris | last post by:
Hi, I have an SQL Query that loads a SQLDataReader object. The returned record has bit datatype columns that I use to provide true/false values. I can't get a definite answer googling on...
1
by: excelleinc.com | last post by:
Hi, I'm trying to check if field contains NULL value in MSSQl 2000 database but keep receiving error. asp.net code: If Trim(HLSQLDSet.Tables("mfglinks").Rows(15).Item(0)) Is Null Then...
5
by: Andy G | last post by:
I'm getting this error...Operator is not valid for type 'DBNull' and string "". What is happening is that I'm calling a stored procedure to use the email address to recover a forgotten username. ...
2
by: Andy G | last post by:
How can I check this for null? dsPrsn.Tables(0).Rows(0)("WORK_STATE") I tried If IsDbNull(dsPrsn.Tables(0).Rows(0)("WORK_STATE")) Then it seems not too work. I am attempting to check this field...
4
by: subaruwrx88011 | last post by:
So I pressed tab-enter and posted an incomplete topic, sorry. I have imported information into a database using a file. It is tab delimited and for NULL fields I have NULL. example file.txt...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.