简体   繁体   中英

Capture dot for Input type number in Android Device and Browser

In below snippet code, If i give inputs "9999999999" and "9999999999." in console log both displays the same result "999999999" with out dot.

Am trying to capture dot symbol even after number. Here, only input type number is allowed cannot use text or any other type.

Text box is for number

Number: <input type='number' id=txtNumber'/>

And it's javascript function

$(document).ready(function() {  
   $('#txtNumber').keyup(function () {
     var numbers = $(this).val(); console.log("numbers", numbers);
   });
});

code at JSFiddle

Please help to find solution.

The reason is you set type='number' . I suggest you set type='text' and use regular expressions to check if the value in the input is a number(include dot) or not:

  $(document).ready(function() {  
       $('#txtNumber').keyup(function () {
        if ($(this).val().match(/^[0-9]*\.?[0-9]*$/)){ //check if your value is number or not
         var numbers = $(this).val(); console.log("numbers", numbers);
         }
       });
    });

You can use a function to do this:

$(document).ready(function () {
        $('#txtNumber').keyup(function () {
            var numbers = $(this).val(); console.log("numbers", numbers);
            // dump(numbers);
            numbers = parseInt(numbers).toFixed(2);
            console.log(numbers);
        });
    });

It will give you your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM