/    Sign up×
Community /Pin to ProfileBookmark

Problem(s) on number auto-format function.

The function formatCurrency is used to auto-format a numeric value with thousands separators and a decimal separator.

It works, but still has the following problems:

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

Problem 1.

The function automatically inserts the decimal separator

[code]
onKeyPress=”return(currencyFormat(this,’,’,’.’,event))” //(where ‘.’ is the decsep)
[/code]

This is a problem because when a user tries to write 1000 (one thousand) it is formatted as 10.00.

I need to modify the function so that it inserts the digit decSep only when the user digits the decSep key.

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

Problem 2.

As it is the function only supports numbers with two decimals, but it would be useful to pass the decimal position as an argument of the function itself.

I tried to add the argument decSepPos (decimal separator position)
and modify the function in these way:


————

Line 7:

[code]
function currencyFormat(fld, milSep, decSep, e) {
[/code]

Becomes

[code]
function currencyFormat(fld, milSep, decSep, decSepPos, e) {
[/code]


———–

line 44:

[code]
fld.value += decSep + aux.substr(len – 2, len);
[/code]

becomes

[code]
fld.value += decSep + aux.substr(len – decSepPos, len);
[/code]

————–
line 59:

[code]
onKeyPress=”return(currencyFormat(this,’,’,’.’,event))”>
[/code]

becomes

[code]
onKeyPress=”return(currencyFormat(this,’,’,’.’,’3′,event))”>
[/code]

where ‘3’ is clearly an example.
————————————————-

This modification works well, but only when decSepPos is >=2.
If the passed value is 1 or 0 the function stops working well.

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°

Problem 3.

In firefox the TAB key does not work (while it works in ie).
This implies not to be able to switch from a textbox to another object using the tab key.I tried to add line 16 the following line:

[code]
if (whichCode == 9) return true; // (9 is the keycode of TAB key)
[/code]

Without obtaining any result.

°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
Here is the sample page I’im working on
Thanks to everybody will support me on one or more of these problems.

Paolo

§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§

[code]
<HTML>

<HEAD>

<SCRIPT LANGUAGE=”JavaScript”>

function currencyFormat(fld, milSep, decSep, e) {
var sep = 0;
var key = ”;
var i = j = 0;
var len = len2 = 0;
var strCheck = ‘0123456789’;
var aux = aux2 = ”;
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true; // Enter
if (whichCode == 8) return true; // Delete
key = String.fromCharCode(whichCode); // take the key from keycode
if (strCheck.indexOf(key) == -1) return false; // Unvalid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != ‘0’) && (fld.value.charAt(i) != decSep)) break;
aux = ”;
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = ”;
//if (len == 1) fld.value = ‘0’+ decSep + ‘0’ + aux;
//if (len == 2) fld.value = ‘0’+ decSep + aux;
if (len > 2) {
aux2 = ”;
for (j = 0, i = len – 3; i >= 0; i–) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = ”;
len2 = aux2.length;
for (i = len2 – 1; i >= 0; i–)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len – 2, len);
}
return false;
}

</script>

</HEAD>

<BODY>

<form>
Input value:
<input type=”text” name=”test” length=”15″ onKeyPress=”return(currencyFormat(this,’,’,’.’,event))”>
</form>
</BODY>
</HTML> [/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKEROct 17.2008 — See if you can use this to format your currency values:
[code=php]
function addcommas( sValue ) {
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
while(sRegExp.test(sValue)) { sValue = sValue.replace(sRegExp, '$1,$2'); }
return sValue;
}
[/code]

Post back if you have problems with it. ?
×

Success!

Help @paolo_ang spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...