473,763 Members | 4,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check null or 0

I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.

Regards,

S

Feb 27 '06 #1
18 4783
sta...@gmail.co m wrote:
I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.

Regards,

S


try this:

forecast = forecast + parseInt(f[i].value);

http://www.w3schools.com/jsref/jsref_parseInt.asp

- JS

Feb 27 '06 #2

st****@gmail.co m wrote:
I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);


The use of eval is most often misused and unnecessary.

Try the following to create your sum:

forecast += +f[i].value;

Feb 27 '06 #3
wrote on 27 feb 2006 in comp.lang.javas cript:
I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.


eval() is evil, and I see no reason why you are using it here.

===========

If you mean the following:

var forecast = 7
var xxx = 8
var yyy ='xxx'
forecast = forecast + eval(yyy);
alert(forecast) //15

it is much better to use:

var forecast = 7
var xxx = 8
var yyy ='xxx'
forecast = forecast + window[yyy];
alert(forecast) //15

now what if xxx is not initialized or NaN:

var forecast = 7
var xxx
var yyy = 'xxx'
if (!window[yyy]||isNaN(window[yyy])) {
var a = 0
yyy = 'a'
}
forecast = forecast + window[yyy];
alert(forecast) //7

will this help?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 27 '06 #4
st****@gmail.co m wrote:
I'm using the following code to create a sum:
forecast = forecast + eval(f[i].value);
Don't use eval.
http://www.JavascriptToolbox.com/bestpractices/
This only works if "f[i].value" contains a number.
Can someone please help me so that it also works if "f[i].value" is
empty or contains text.


Well, what do you want it to do?
Should it treat non-number values as 0?
Ignore them?
Throw up an error?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 27 '06 #5
st****@gmail.co m said the following on 2/27/2006 1:30 PM:
I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.


Simple. Drop the eval:

forecast = forecast + f[i].value;

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 28 '06 #6
VK

st****@gmail.co m wrote:
I'm using the following code to create a sum:

forecast = forecast + eval(f[i].value);

This only works if "f[i].value" contains a number.

Can someone please help me so that it also works if "f[i].value" is
empty or contains text.


forecast += +(f[i].value)||0;

P.S. I decided to participate in the squeezcrypt rally :-) I did not
play squeezcrypt for rather long so just cheking my fingers are stll
flexible :-)

The decrypter version (if someone curious) is:

var n = parseInt(f[i].value, 10);
if (isNaN(n)) {
forecast += 0;
}
else {
forecast += n;
}

Feb 28 '06 #7
VK wrote:
forecast += +(f[i].value)||0;
There's no need for the parenthesis.
P.S. I decided to participate in the squeezcrypt rally :-) I did not
play squeezcrypt for rather long so just cheking my fingers are stll
flexible :-) [...] The decrypter version (if someone curious) is:


I saw a lot of people criticizing you here, I thought they just hated
you, but after reading this message I can see they are totally right
hahaha ;]
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 28 '06 #8
VK

Jonas Raoni wrote:
VK wrote:
forecast += +(f[i].value)||0;
There's no need for the parenthesis.


Right.

forecast+=+f[i].value||0;

As I said I did not play squeezcrypt for a while :-)

I saw a lot of people criticizing you here, I thought they just hated
you, but after reading this message I can see they are totally right
hahaha ;]


Oh boy, did we get hurt? :-( ;-)
I thought there were enough of smilies in my post to prevent that -
plus nothing personal.

So what the exact reason of *your* hate is? That I explained what did
forecast+=+f[i].value||0;
do and mean?

It's hard to believe, I know, but it is not so obvious at all to an
average user. Plus some people may prefer to use the conventional
isNaN() check so I thought necessary to mention it.

Feb 28 '06 #9
VK wrote:
Jonas Raoni wrote:
I saw a lot of people criticizing you here, I thought they just hated
you, but after reading this message I can see they are totally right
hahaha ;]
Oh boy, did we get hurt? :-( ;-)


Hahahaha, no! We'll never get hurt, this isn't an arena, but a place to
spend some free time, I don't take anything personally ;]

Even disliking such unuseful discussions, I can't resist to my agressive
instinct, so if you can, stop this thread by not answering, it's already
over, the question got an answer =]
So what the exact reason of *your* hate is? That I explained what did
forecast+=+f[i].value||0;
do and mean?
Yeah, that's the reason, explaining how the script works looks idiot for
me, I explain only if the person asks for explanation.
It's hard to believe, I know, but it is not so obvious at all to an
average user. Plus some people may prefer to use the conventional
isNaN() check so I thought necessary to mention it.


You're right, but explaining the script is a kind of insult for the
idiot people like me haha. I assume everybody has a nice knowledge about
the language, so nothing needs explanation.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 28 '06 #10

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

Similar topics

1
5306
by: Martin | last post by:
Hello Everybody! I have a POLINE table on a SQL Server 2000 DB. Before I update the record I need to check that either field, STORELOC or WONUM has data on it. If both fields are NULL I would like to send a message letting the user know that either fields needs data before they can save the record. If any of the fields have data then, it is OK to save the record. Could you please let me know how to accomplish this? An example will
2
7186
by: Doug Baroter | last post by:
Hi, DDLs and DMLs: create table #job (jobID int identity(1,1) primary key, jobName varchar(25) unique not null, jobEndDate dateTime, jobComplete bit default(0), check (( is null and = 0) OR ( is not null and = 1))); Q1 with check constraint: sample dagta
4
10263
by: Laphan | last post by:
Hi all In my functions I'm using a double-check all the time to trap if a value has nothing in it and my question is do I need this double-check. My check line is: IF IsNull(xxx) OR LEN(xxx) = 0 THEN blah blah
3
1684
by: GS | last post by:
Hi, I have following value which I need to check wether it's null or not. Issue is that StartPrice itself can be a null and checking (myItem.StartPrice.Value == null) do not produce true. How do I check for null in this kind of case. I don't want to check StartPrice for null and then Value for null again as one option. I just want to check Value for null. Thanks, Greg
3
6322
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql, which is called once the user clicks the OK button on this dialog. try { int rows = cmd.ExecuteNonQuery(); } catch(SqlException se)
1
1417
by: shapper | last post by:
Hello, I have 3 different properties in a class. The properties are of types: Mail.MailPriority, Mail.MailAddress and Mail.MailAddressCollection I wan to check if the properties were defined, i.e., if they are null or not. I was able to work this out for properties of type boolean and string
16
5665
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
3
6201
by: Helen Wheels | last post by:
Can we use parentheses in a check constraint in MS-SQL-server DDL? e.g. I'm having a problem with the following statement: ALTER TABLE . ADD CONSTRAINT CHECK (( IS NULL AND IS NULL) OR ( IS NOT NULL AND IS NOT NULL AND IS NOT NULL));
14
17196
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL) tetromino = new TetrominoI; This continuously replaces the tetromino as expected, but if I take
2
14659
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
0
9386
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
10144
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
9997
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...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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
6642
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
5270
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...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.