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

How to make formula work even if textbox doesn't exist

P: n/a
I'm using the following formula to sum values into a textbox. How can I
make it work even if the underlying textbox doesn't exist? It should
then be treated as 0.

document.getElementById('_ctl2_lblForecastYearSumR evenues').innerHTML =
CheckValue(document.getElementById('_ctl2_txtCurre ntYearRevenues').value);
function CheckValue(amount) {
if(amount=="" || isNaN(amount))
return 0;
else
return parseInt(amount);
}

Regards,

S

Jun 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
st****@gmail.com wrote:
CheckValue(document.getElementById('_ctl2_txtCurre ntYearRevenues').value);


Instead, pass a reference to the element itself:

CheckValue(document.getElementById('_ctl2_txtCurre ntYearRevenues');

And then adjust your function:

function CheckValue(field) {
if(typeof(field)=="undefined" || field==null) {
return 0;
}
var amount = field.value;
if (amount==null || amount=="" || isNaN(amount)) {
return 0;
}
else {
return parseInt(amount,10) || 0;
}
}

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 26 '06 #2

P: n/a
JRS: In article <e7*********@news1.newsguy.com>, dated Mon, 26 Jun 2006
09:12:48 remote, seen in news:comp.lang.javascript, Matt Kruse
<ne********@mattkruse.com> posted :
function CheckValue(field) {
if(typeof(field)=="undefined" || field==null) {
return 0;
}
var amount = field.value;
if (amount==null || amount=="" || isNaN(amount)) {
return 0;
}
else {
return parseInt(amount,10) || 0;
}
}


If unary + is used instead of parseInt, then the preceding tests for ""
& null may be unnecessary. It will then, ISTM, accept Infinity.

ISTM simpler to check with a befitting RegExp before conversion.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of 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.
Jun 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.