Empty field giving me undefined

T

tshad

I have a function that formats currency to add commas and $.

But I also check to see if the field is empty and if it is I return.

The problem is that the field now contains the word "undefined". I don't
want to have anything in this case.

Why is that word showing up and how do I tell it to do nothing?

function formatCurrency(num) {
if (num.Length == 0)return;
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

I call it as:

onBlur="this.value= formatCurrency(this.value)"

Thanks,

Tom
 
L

Lee

tshad said:
I have a function that formats currency to add commas and $.

But I also check to see if the field is empty and if it is I return.

The problem is that the field now contains the word "undefined". I don't
want to have anything in this case.

Why is that word showing up and how do I tell it to do nothing?

function formatCurrency(num) {
if (num.Length == 0)return;

There is no attribute named "Length".
It's "length".

if(!num.length) return;
 
M

Mick White

tshad said:
I have a function that formats currency to add commas and $.

But I also check to see if the field is empty and if it is I return.

The problem is that the field now contains the word "undefined". I don't
want to have anything in this case.

Why is that word showing up and how do I tell it to do nothing?

function formatCurrency(num) {
if (num.Length == 0)return;

if(!num.length) return "";

The rest of this function is pretty bad
num = num.toString().replace(/\$|\,/g,'');
It's going to be a string:
num = num.replace(/[$,]/g,'');

if(isNaN(num))
num = "0";
This could be a problem (with division by zero).
sign = (num == (num = Math.abs(num))); sign=num<0?"-":"";

num = Math.floor(num*100+0.50000000001);
Why not the following?
num = num*100+""
num=num.substring(0,num.length-2)+"."+num.substring(num.length-2);
return sign+"$"+num;

Mick
 
M

Michael Winter

I have a function that formats currency to add commas and $.

Same here. :)

/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* t - Thousands separator (defaults to none).
* d - Decimal separator (defaults to '.').
*
* Outputs a number of the form cntnnntnnndnn
*
* For example, toCurrency(142635.7, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, t, d) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.round(n));
var j, i = '', f; c = c || ''; t = t || ''; d = d || '.';

while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = t + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + d + f;
}

It's predominantly string-oriented, so floating-point errors are not a
concern. However, it takes a number as it's first argument, not a
string so you'll have to do your error checking beforehand.
The problem is that the field now contains the word "undefined". [...]
Why is that word showing up and how do I tell it to do nothing?

function formatCurrency(num) {
if (num.Length == 0)return;

The length property begins with a lowercase L, so the function always
ends here. Unlike other languages, no return value does actually
return a value: undefined. This is then type-converted into the
string, 'undefined', when you assign to the value property.
num = num.toString().replace(/\$|\,/g,'');

The num variable is already a string, to the toString call is unnecessary.
num = Math.floor(num*100+0.50000000001);

See, this is where string manipulation is preferred - no kludges needed.

Hope that helps,
Mike
 
T

tshad

Michael Winter said:
Same here. :)

/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* t - Thousands separator (defaults to none).
* d - Decimal separator (defaults to '.').
*
* Outputs a number of the form cntnnntnnndnn
*
* For example, toCurrency(142635.7, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, t, d) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.round(n));
var j, i = '', f; c = c || ''; t = t || ''; d = d || '.';

while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = t + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + d + f;
}

It's predominantly string-oriented, so floating-point errors are not a
concern. However, it takes a number as it's first argument, not a string
so you'll have to do your error checking beforehand.

I am going to look at your function as it does look more clean.
The problem is that the field now contains the word "undefined". [...]
Why is that word showing up and how do I tell it to do nothing?

function formatCurrency(num) {
if (num.Length == 0)return;

The length property begins with a lowercase L, so the function always ends
here. Unlike other languages, no return value does actually return a
value: undefined. This is then type-converted into the string,
'undefined', when you assign to the value property.

So how would I return at the top of the function if there is nothing in the
field?

Should I not return a value and just change the value inside of the
function? If this is the case, would I call the function something like:

onBlur=" return formatCurrency(this.value)"

or

onBlur="javascript:formatCurrency(this.value)"

Not really sure why I would do it this way, but I see others call these
ways.

Thank,

Tom
 
M

Michael Winter

So how would I return at the top of the function if there is nothing in the
field?

Return an empty string or '$0.00'. Whichever you prefer.
onBlur="javascript:formatCurrency(this.value)"

The "javascript:" part would be unnecessary. Most user agents would
interpret that as nothing more than a statement label. Though IE would
attach some meaning to it, it would only be useful if you used
VBScript earlier in the document (which would be a bad idea on the Web).

Mike
 
T

tshad

Michael Winter said:
Return an empty string or '$0.00'. Whichever you prefer.

It worked fine when I did:

if (num.length == 0)return "";
The "javascript:" part would be unnecessary. Most user agents would
interpret that as nothing more than a statement label. Though IE would
attach some meaning to it, it would only be useful if you used VBScript
earlier in the document (which would be a bad idea on the Web).

Then what is the difference between:

onBlur="javascript:formatCurrency(this.value)"

onBlur="return javascript:formatCurrency(this.value)"

Thanks,

Tom
 
L

Lee

tshad said:
It worked fine when I did:

if (num.length == 0)return "";


Then what is the difference between:

onBlur="javascript:formatCurrency(this.value)"

onBlur="return javascript:formatCurrency(this.value)"

The second is a syntax error. The label ("javascript:" in this case),
must appear first on the line.

If nobody else has mentioned it, it's a bad idea to use the onBlur
event of a text field, since there are so many uncontrollable and
unexpected things that can happen to cause a field to lose focus
before the user is done typing in it. Using onChange is cleaner.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed,
6 Apr 2005 20:00:12, seen in Mick White
Why not the following?
num = num*100+""
num=num.substring(0,num.length-2)+"."+num.substring(num.length-2);
return sign+"$"+num;

You must be a newcomer ... <g> search back a few years in the group.

That's OK for num = 1.07 & 1.00, dubious for num = 0.99 & 0.90 (decimal
points should always be surrounded by digits), numerically wrong for
0.09 and 0.08, and amazing for 0.07, for which I get 7.0000000000000.01
..

For reliable rounding, see the newsgroup FAQ and <URL:http://www.merlyn.
demon.co.uk/js-round.htm>.

For the insertion of thousands separators in numeric strings, see
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Out> and in particular
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Money>, after another
MW.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top