Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problem(s) on number auto-format function.

    Thanks in advance to everyone that will give me help for these questions.

    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)
    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) {
    Becomes
    Code:
    function currencyFormat(fld, milSep, decSep, decSepPos, e) {
    -----------
    line 44:
    Code:
    fld.value += decSep + aux.substr(len - 2, len);
    becomes
    Code:
    fld.value += decSep + aux.substr(len - decSepPos, len);
    --------------
    line 59:
    Code:
    onKeyPress="return(currencyFormat(this,',','.',event))">
    becomes
    Code:
    onKeyPress="return(currencyFormat(this,',','.','3',event))">
    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)
    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Your script is extremely cumbersome! The following will format your currency:-

    Code:
    <script type = "text/javascript">
    
    var num = 1234567890123.45    //works with negative numbers as well and 3 decimal places so good for currency
    
    document.write('<span id="count"></span>');
    
    document.getElementById('count').innerHTML = addCommas(String(num));
    
    function addCommas(nStr) {
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    return nStr;
    }
    
    </script>

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.



    When trouble arises and things look bad, there is always one individual who perceives a solution and is willing to take command. Very often, that individual is crazy.
    Last edited by Philip M; 10-16-2008 at 06:50 PM.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    without a loop:
    Code:
    function dollarize(n){
      var s = n.toString();
      var so = s.split(".");
      s=Number(so[0]).toLocaleString();
     return "$" + s + "."+ (so[1] || "00" );
    }
    
    alert(  dollarize(20000000)  ) // $20,000,000.00
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    paolo (10-17-2008)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,729
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Quote Originally Posted by rnd me View Post
    without a loop:
    Code:
    function dollarize(n){
      var s = n.toString();
      var so = s.split(".");
      s=Number(so[0]).toLocaleString();
     return "$" + s + "."+ (so[1] || "00" );
    }
    
    alert(  dollarize(20000000)  ) // $20,000,000.00

    Alas, 20000000 returns $20,000,000.00.00 and 20000000.99 returns $20,000,000,00.99.

    Suggest:-

    Code:
    <script type = "text/javascript">
    
    function dollarize(n){
    var s = n.toString();
    var so = s.split(".");
    s=Number(so[0]).toLocaleString();
    s = s.replace(/\..+/,"");
    return "$" + s + "."+ (so[1] || "00" );
    }
    
    alert(  dollarize(20000000)  ) // $20,000,000.00
    </script>

  • Users who have thanked Philip M for this post:

    paolo (10-17-2008)

  • #5
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted.
    Hi Philip,

    In Italian newsgroups this formula is very common; as far as I know nobody has reported to get that impression. Anyway I'll follow your suggestion in my future messages.

    Paolo


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •