Comma and dot in input form

Discussion in 'JavaScript' started by kensol, Mar 30, 2008.

  1. #1
    Hello all!

    I am looking for a way to make my js interpret a comma (,) the same way it does a dot (.) in an input form. I am using the input forms for decimals that are being calculated, and here in Norway it's common to use the comma as a decimal separator.

    Thx

    Ken
     
    kensol, Mar 30, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just replace comma with nothing.
    
    str = "32,200.100";
    str = str.replace(",","");
    numFloat = parseFloat(str);
    alert(numFloat);
    
    Code (markup):
    That should do it.

    Good luck :)
     
    Morishani, Mar 30, 2008 IP
  3. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Maybe the best way to use eval() function?

    it seems to me that eval("0.0001") is the same with eval("0,0001")...
     
    So1, Mar 30, 2008 IP
  4. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Never use eval function, it is a major security breach.
     
    Morishani, Mar 30, 2008 IP
  5. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    For this problem it is not security breach. Otherwise maybe u should to explain why it's so? What've to happen?
     
    So1, Mar 30, 2008 IP
  6. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    User can enter javascript code, with some pages it maybe harmful.
    Anyway, rule no.1 - don't use eval.
     
    Morishani, Mar 30, 2008 IP
  7. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Anyway, i can enter ANY javascript code in browser.

    in address string enter
    javascript: document.body.contentEditable='true'; document,designeMode='on'; void 0;
    Code (markup):
    instead of address string. Work? Work! Enter
    javascript:alert('hahaha'); void 0;
    Code (markup):
    Work? So, u can use eval() function in the same way.
    javascript: alert(eval('0.001')); void 0;
    Code (markup):
     
    So1, Mar 30, 2008 IP
  8. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You could try something like this:

    
    var v = document.getElementById("TextBox").value;
    if (v.indexOf(",") > -1) { v = v.replace(/,/, "."); }
    var n = parseFloat(v);
    
    Code (markup):
    But it won't handle numbers which have both a comma and a dot, such as:

    1,000,000.00

    or

    1.000.000,00

    You could make it check to see if it contains both characters, and if so, force the user to re-enter the number using one or the other.
     
    vpguy, Mar 30, 2008 IP
  9. kensol

    kensol Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you all for replies!

    I think I'll go with vpguy's suggestion. I'm only working with one decimal for this one (XX,X) so I'll be fine.

    Again, thanks.

    Ken
     
    kensol, Mar 31, 2008 IP