0

How to create an input type number using this format?

Example : when I start writing this number 1000, it should be generated to 1'000.

Example 2: 10000 to 10'000

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Iyadh
  • 11
  • 2

4 Answers4

1

i'm using this plugin to make the number field auto masking. Very easy to use. Try this one. Give feedback if you can't implement it.

Jquery number masking plugin repository

Examaple usage. After download plugin file, add this code into your js section code.

$('selector_element_to_mask').number(true, 0);
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
1

I think this is it.

$(document).ready(function(){
  $('#num').keyup(function(){
    var numbers = $(this).val().replace("'","");
    var newNum = "";
    var digitCount = 0;
    
    for(var i = numbers.length-1; i>=0; i--){
      
      if(digitCount==3){
        newNum+="'";
        digitCount=0;
        }
      newNum += numbers.charAt(i);
      if($.isNumeric(numbers.charAt(i)))
         digitCount++;
      
    }
    $(this).val(newNum.split("").reverse().join(""));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<input type="text" id="num">
Ray Lionfang
  • 689
  • 6
  • 17
1

Try this It will surly help you:

var num = 1000000;
console.log(formatNumber(num))
function formatNumber (num) {
    return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1'")
}

DEMO with textfield http://jsfiddle.net/65y2bbrm/1/

PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
0

I think you are looking for input masking posting a similar question though a bit different but I am sure it will point you in the right direction.

javascript or jquery Input text number only and auto masking

Community
  • 1
  • 1
usamazf
  • 3,195
  • 4
  • 22
  • 40