Showing posts with label jquery format number two decimal places. Show all posts
Showing posts with label jquery format number two decimal places. Show all posts

Tuesday, January 12, 2021

Textbox format number with two decimal places

 There can be many ways to display a number formatted in a textbox. Here I have used jQuery to achieve the expected result.


<script>

$(function () {

    $('#txtAmount').on('input', function(e) {

        if (/^(\d+(\.\d{0,2})?)?$/.test($(this).val())) {

            $(this).data('oldValue', $(this).val());

        } else {

            $(this).val($(this).data('oldValue') || '');

        }

    }).trigger('input');  

});

</script>

HTML

<input type="text"  id="txtAmount" placeholder="Enter Amount" />


Cheers

Samitha