Validate input decimal point

Input a number with one decimal point only

by Faisal Khan Janjua

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
Enter a number with one(1) decimal point only<br />
<input type="text" id="decimalPt"> <br />

<input type="text" class="decimalPt roundUp" > <br />

JavaScript

$('#decimalPt, .decimalPt').keypress(function(evt) {
  evt = (evt) ? evt : window.event;
  var charCode = (evt.which) ? evt.which : evt.keyCode;
  if (charCode == 8 || charCode == 37) {
    return true;
  } else if (charCode == 46 && $(this).val().indexOf('.') != -1) {
    return false;
  } else if (charCode > 31 && charCode != 46 && (charCode < 48 || charCode > 57)) {
    return false;
  }
  return true;
});

$('.roundUp').blur(function() {
  var value = parseFloat($(this).val());
  if (!isNaN(value)) {
    $(this).val(parseFloat(value).toFixed(2));
  }
});