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

S

staeri

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_lblForecastYearSumRevenues').innerHTML =
CheckValue(document.getElementById('_ctl2_txtCurrentYearRevenues').value);


function CheckValue(amount) {
if(amount=="" || isNaN(amount))
return 0;
else
return parseInt(amount);
}

Regards,

S
 
M

Matt Kruse

CheckValue(document.getElementById('_ctl2_txtCurrentYearRevenues').value);

Instead, pass a reference to the element itself:

CheckValue(document.getElementById('_ctl2_txtCurrentYearRevenues');

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;
}
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 26 Jun 2006
09:12:48 remote, seen in Matt Kruse
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top