Solved

Javascript convert null to zero

Posted on 2009-07-16
16
2,269 Views
Last Modified: 2012-06-21
I am reading 3 values off a form on a webpage (all integers). These fields may or may not be filled in.
I want to add them together (field1 + field2 + field3 = total).
I have gotten this working fine excepting that if 1 of them is not filled in then the value comes through incorrectly.
Is there a Javascript function similar to the SQL  isnull() function ?
I want to say "if the value is not a number then make it zero" so that nulls are treated as zero.

Is there a neat way to do this?
0
Comment
Question by:Jon-G
  • 5
  • 5
  • 3
  • +3
16 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 24870472
Try
total = parseInt(field1) + parseInt(field2) + parseInt(field3);
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24870492
if (field ==null){//....}
or try isNan function http://www.w3schools.com/jsref/jsref_isNaN.asp
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24870494
You can use:
IsNaN(<value>) ? 0 : <value>

If you wish to use a function and specifically look for a null then:
function IsNull(value, default)
{
  return value == null ? default : value;
}
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24870533
If they are integer values, use something like this;


If they are floating point values, change:

parseInt( x, 1 )

to

parseFloat( x )
function check( x ) {

  return x ? parseInt( x, 10 ) : 0;

}
 

total = check( value1 ) + check( value2 ) + check( value3 );

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24870548
sigh...

If they are floating point values, change:

parseInt( x, 10 )

to

parseFloat( x )
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24871304
Just multiply each value by 1

total = 1*field1 + 1*field2 + 1*field3
0
 

Author Comment

by:Jon-G
ID: 24872475
I understand what this does but I am unfamiliar with the syntax in the return statement.
What does the "?" and the ":" do ?
I can't understand what the condition is that you are using to decide on the return value.



function check( x ) {
  return x ? parseInt( x, 10 ) : 0;
}
 
total = check( value1 ) + check( value2 ) + check( value3 );
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24872659
The way this statement works is this:

The value of x is check, and if it is non-null, non 0, not empty, or true, the expression between the ? and the : is evaluated.  If the result is null, 0, empty, or false, the expression after the : is evaluated.

So, for non-null, non 0, not empty, or true, the value returned will be the result of evaluating:

  parseInt( x, 10 )

which means that the parseInt() function will try to convert the value of x to an integer using a radix of 10 (i.e., using base 10 number system).

otherwise, the result returned will be

  0

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 50 total points
ID: 24872687
So, the output for the following code is:

123
<html>

<body>
 

<script type="text/javascript">
 

function check( x ) {

  return x ? parseInt( x, 10 ) : 0;

}
 

var value1 = null;

var value2 = '123';

var value3 = '';
 

document.write( check( value1 ) + check( value2 ) + check( value3 ) );
 
 

</script>
 

</body>

</html>

Open in new window

0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 75 total points
ID: 24875246
Multiplying by 1 is so much easier

<script>

  var value1 = null;
  var value2 = '123';
  var value3 = '';

  alert( value1  + value2  + value3 );
  alert( 1*value1  + 1*value2  + 1*value3 );

</script>
0
 

Author Comment

by:Jon-G
ID: 24875727
I tested them out testing for both out and they both worked great.
I then tested them both by including both a blank string and a string with a space in it.

Multiplying by 1 still worked
function check( x )  - returned NaN which makes sense.

Why does multiplying by 1 actually work in this case ? I would think that 1xnull would throw an error.
I'm quite surprised to see that it doesn't. I think you have quite a well kept secret here :)

0
 

Author Comment

by:Jon-G
ID: 24875745
Will multiplying by 1 still work if I am using "+=" ?
It is much neater in the context of my application (the example below uses shorter names and less values than my actual app which is a form in Microsoft CRM)

var pointSum;
pointSum = 0;

pointSum += crmForm.all.field1.DataValue ;
pointSum += crmForm.all.field2.DataValue ;
pointSum += crmForm.all.field3.DataValue ;
0
 

Author Comment

by:Jon-G
ID: 24875749
Sorry. Forgot to add in the "*1"

pointSum += crmForm.all.field1.DataValue*1 ;
pointSum += crmForm.all.field2.DataValue*1 ;
pointSum += crmForm.all.field3.DataValue*1 ;
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 24875907
yep the above will work.  The multiply by 1 idea is works cause of the Javascript rules of mix types being combined with *. Basically the rules attempt to return a number whenever they can.
0
 

Author Closing Comment

by:Jon-G
ID: 31604292
Thanks for the help guys.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24877914
Thanks for the assist.

Good luck & have a great day.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now