473,413 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Empty field giving me undefined

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
Jul 23 '05 #1
8 1839
tshad wrote:
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 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

Jul 23 '05 #2
On 06/04/2005 20:27, tshad wrote:
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
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;

Jul 23 '05 #4

"Michael Winter" <m.******@blueyonder.co.invalid> wrote in message
news:Hr***************@text.news.blueyonder.co.uk. ..
On 06/04/2005 20:27, tshad wrote:
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.


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

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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #5
On 06/04/2005 22:08, tshad wrote:
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
"Michael Winter" <m.******@blueyonder.co.invalid> wrote in message
news:xo****************@text.news.blueyonder.co.uk ...
On 06/04/2005 22:08, tshad wrote:
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.


It worked fine when I did:

if (num.length == 0)return "";
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).


Then what is the difference between:

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

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

Thanks,

Tom
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #7
Lee
tshad said:

"Michael Winter" <m.******@blueyonder.co.invalid> wrote in message
news:xo****************@text.news.blueyonder.co.u k...
On 06/04/2005 22:08, tshad wrote:
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.


It worked fine when I did:

if (num.length == 0)return "";
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).


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.

Jul 23 '05 #8
JRS: In article <gd******************@twister.nyroc.rr.com>, dated Wed,
6 Apr 2005 20:00:12, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
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.

--
© 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.
Jul 23 '05 #9

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
11
by: WindAndWaves | last post by:
Hi Gurus What I would like to do is to setup a little form where people can put in a date (e.g. Day: Month..... Year , where ... is user input) and subsequently, will take them through to an...
5
by: Thejo | last post by:
Hi all, I started programming in PHP recently and have a query about empty $_POST arrays. I can see two scenarios when this could happen. 1. When some tries to directly load the page to which...
11
by: Bradford Chamberlain | last post by:
I work a lot with multidimensional arrays of dynamic size in C, implementing them using a single-dimensional C array and the appropriate multipliers and offsets to index into it appropriately. I...
4
by: whisher | last post by:
Hi. I'm taking my first steps on regex I set up this simple function to check if a form field is empty or with only space. var onlySpaceRegexp = /^\s*$/; function isEmpty(val) { if...
6
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, Thought this would be simple! If I want to validate a field that can contain "z" followed by 3 digits OR it's a completely empty string, how do I do that? The z123 bit is simple, but the empty...
2
by: Twinkle | last post by:
Can anyone please help me with the following. I have an empty Varchar column for one table. I need to convert the datatype to date now. But i need the field to be empty. How can this be done. ...
8
code green
by: code green | last post by:
I have been working with a script I have inherited that uses the ADODB class. I want to run a query that checks a record is unique across the primary key and date field in a MsSql DB before...
7
by: Moschops | last post by:
In moving some code from VS6 to VS2008 (bear with me, this is not a VS question, I'm just setting context), we find new crashes that weren't there before and we think they're related to trying an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.