By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Checking for null value in a field

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.