473,761 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 TotalSumFundRai sVol = 0;
var field = 0;
//Search through the entire array of form elements and
//locate all fields that include the word "Fundrais_v ol"
//in their name. When found, add the value of that form
//element to TotalSumFundRai sVol
for (var i = 0; i<document.form s[0].elements.lengt h; i++) {
field = document.forms[0].elements[i].name.indexOf(' Fundrais_vol');
if (field > -1 && field != null) {
TotalSumFundRai sVol +=
eval(document.f orms[0].elements[i].value);
}
}
document.forms[0].Fndraising_Vol _Total.value =
TotalSumFundRai sVol;
}
Jul 23 '05 #1
4 11789
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 TotalSumFundRai sVol = 0;
var field = 0;
//Search through the entire array of form elements and
//locate all fields that include the word "Fundrais_v ol"
//in their name. When found, add the value of that form
//element to TotalSumFundRai sVol
for (var i = 0; i<document.form s[0].elements.lengt h; i++) {
field = document.forms[0].elements[i].name.indexOf(' Fundrais_vol');
if (field > -1 && field != null) {
TotalSumFundRai sVol +=
eval(document.f orms[0].elements[i].value);
}
}
document.forms[0].Fndraising_Vol _Total.value =
TotalSumFundRai sVol;
}


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.leng th; i < n; ++i )
{
element = collection[ i ];
if( -1 != element.name.in dexOf( '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.valu e = 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.******@blueyo nder.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.******@bluey onder.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.v alue)) total += +RegExp.$1
or
OK = /^(\d{1,6})$/.test(element.v alue)
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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
356
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: šššššššštypedefšstructš{ ššššššššššššššššintššššši; ššššššššššššššššcharššššname; šššššššš}šMY_TYPE; ššššššššconstšcharš*
2
3407
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 how to test if this field is null. I get casting errors, and can't check for null on a null data value errors
1
1912
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 URLSurgeS.Visible = "False" End If
5
1798
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. If the email does not exist then I get the above error. After I execute the stored procedure I try and apparenty this is not catching the error. Should I use a Try...Catch? What is the correct way to check for nulls in ASP.NET? If Not...
2
1773
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 in my data set to see if it is null or not. If it is null I want to set my drop down list to a certain value (where I'm using dropdown.SelectedValue = ). If it is not null I want to set the drop down to the dataset value. Here is what the...
4
2302
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 1<tab>Hello World<tab>NULL<tab>5.56 1) Is this a correct way to make sure that this field will be NULL
4
2384
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 exceptions?
3
12306
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 (0) length string, an empty Field, or no value at all - so exactly what is Null? The purpose of this Topic is hopefully to explain what a Null Value is, discuss some peculiarities about Nulls, show how we can detect them, and finally, how to convert...
8
5722
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.