SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question currency mask to forms

    Hi all,

    I'm trying to find a script that automatically completes the comma and point separations to display currency values properly into a form field..

    I found this script on my research but simply don't work when I add to my form..any clue what is missing?

    The js:
    function currencyFormat(objForm, strField, milSep, decSep, e) {
    var sep = 0;
    var sValue = objForm[strField].value;
    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 (Bug fixed)
    key = String.fromCharCode(whichCode); // Get key value from key code
    if (strCheck.indexOf(key) == -1) return false; // Not a valid key
    len = sValue.length;
    for(i = 0; i < len; i++)
    if ((sValue.charAt(i) != '0') && (sValue.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
    if (strCheck.indexOf(sValue.charAt(i))!=-1) aux += sValue.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) sValue = '';
    if (len == 1) sValue = '0'+ decSep + '0' + aux;
    if (len == 2) sValue = '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++;
    }
    sValue = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    sValue += aux2.charAt(i);
    sValue += decSep + aux.substr(len - 2, len);
    }
    return false;
    }

    The form input:
    <input name="valor_declarado" id="valor_declarado" type=text class="cn" value="" size=25 maxlength=50 onKeyPress="return(currencyFormat(document.formnovoequipamento,'valor_declarado',',','.',event))">

    Any help is welcomed..

    Tks.
    Last edited by mjunior; Oct 29, 2006 at 16:07.

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try using this code... i use it alot when doing simple money forms:

    http://javascript.internet.com/forms...cy-format.html

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it works , but..

    Thanks..
    this script works fine...
    actually it comes to the same source I found the one I sent:
    http://javascript.internet.com/forms/auto-currency.html

    the problem with the script you sugested is the onblur event..that makes the field changes their punctuation only after you click someplace else...

    The best solution will be a mix from the two codes..adding the $ prefix like your script and adding the punctuation on the fly like the one I posted..
    (btw..the script preview works on the site...)

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a few errors

    I found a few erros while trying use your script..
    There's no restrictions fot numbers only..
    If you type comma instead ot point for cents separation the field blanks..

    I got also errors trying to insert the value on my DB (number (double) or currency type fields) neighter one of the types of data permits the insert command to work..
    Last edited by mjunior; Oct 30, 2006 at 09:08.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    anyone?

    Anyone can fix my former code or suggest some new one to mask the field entry for currency values?


Bookmarks

Posting Permissions

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