-2

I have created a textbox with id of 'txtDFI' in asp.net.And limited it to accept only float values by using below function

document.getElementById('txtDFI').onkeydown = function() { return(integerAndFloatOnly()) }; 

My problem is that it should accept only two numbers after decimal point..If user entered 3rd number it shuould not accept.. Can anybody help me how can I meet this requirement in javascript...

ManyThanks..

yu_ominae
  • 2,975
  • 6
  • 39
  • 76
deepu
  • 59
  • 7
  • See : http://stackoverflow.com/questions/9967558/only-allow-two-digits-after-decimal-in-textbox – S.K Jul 23 '15 at 07:21
  • possible duplicate of [Allow only 2 decimal points entry to a textbox using javascript or jquery?](http://stackoverflow.com/questions/16666415/allow-only-2-decimal-points-entry-to-a-textbox-using-javascript-or-jquery) – Binke Jul 23 '15 at 07:25

1 Answers1

0

You could have done some research and have easily found out the answer.

Anyways, here you go:-

  1. Assuming that you need to do something with Class name

$('.number').keypress(function(event) {
  if ((event.which != 46 || $(this).val().indexOf('.') != -1) &&
    ((event.which < 48 || event.which > 57) &&
      (event.which != 0 && event.which != 8))) {
    event.preventDefault();
  }
  var text = $(this).val();
  if ((text.indexOf('.') != -1) &&
    (text.substring(text.indexOf('.')).length > 2) &&
    (event.which != 0 && event.which != 8)) {
    event.preventDefault();
  }
});
  CSS part

.number {
  padding: 5px 10px;
  font-size: 16px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="number" />

For Complete reference see here

Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160