skip to Main Content

When user entering 123456 , user will look 123.456 in the input field.

I used my code but this display 123,456

What i want is exactly that Thousand separator is dot (not comma)

My code in link https://jsfiddle.net/bvotcode/tym4qf2r/3/

< form> <label for="ticketNum">Your money</label> <input type="text" oninput="sepratornumberwithdot(this)" placeholder="123.456.789"/> < /form>

function sepratornumberwithdot(input) {let nums = input.value.replace(/,/g, '');if (!nums || nums.endsWith('.')) return;input.value = parseFloat(nums).toLocaleString();}

I tried using Javascript to adjust to dot but didn’t work. Any help would be appreciated.
Thanks!

2

Answers


  1. Normally we use toLocaleString() with one’s country code, like this:

    alert( (123456).toLocaleString('ja-JP') ); // Japanese
    
    
    alert( (123456).toLocaleString('de-DE') ); // German
    

    And so forth…

    But if you’re not happy use the German which does dots!

    Login or Signup to reply.
  2. Change your first regular expression to /[,.]/g so that you get rid of commas and periods.

    Add .replace(/,/g, ‘.’) to the end of your .toLocaleString()

    That will do it. See code snippet below:

    function sepratornumberwithdot(input) {
      let nums = input.value.replace(/[,.]/g, '');
    
      if (!nums || nums.endsWith('.')) return;
      input.value = parseFloat(nums).toLocaleString().replace(/,/g, '.');
    }
    form input {
      /* Keeps your number from moving around */
      text-align: end;
    }
    <form>
      <label for="ticketNum">Your  money</label>
      <input type="text" oninput="sepratornumberwithdot(this)"  placeholder="123.456.789"/> 
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search