-1

I am using a script for my textbox to make sure the user can enter only numbers instead of text.

Here is my textbox:

<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event)" />

And here is my javascript:

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
     return false;
    }
    return true;
} 

I want to upgrade my JavaScript. I want that the textbox accepts a input with a dot . (only 1 dot), like 11.5

What do I need to change in my script so it will be accept the dot and limit this to one?

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
John
  • 904
  • 8
  • 22
  • 56

5 Answers5

0

Try this one

<input type="text" id="txtCheck" onkeypress="AllowDecimalNumbersOnly(this, event)" />

 function AllowDecimalNumbersOnly(Id, Evt) {
            Id.value = Id.value.replace(/[^0-9\.]/g, '');
            if ((Evt.which != 46 || Id.value.indexOf('.') != -1) && (Evt.which < 48 || Evt.which > 57)) {
                Evt.preventDefault();
            }
        }
Shaiwal Tripathi
  • 601
  • 1
  • 12
  • 32
0

JavaScript :

<script>
var isHav=false;
function isNumber(evt) {
   evt = (evt) ? evt : window.event;
   var charCode = (evt.which) ? evt.which : evt.keyCode;
   if(charCode == 46){  
      if(isHav==true){
         return false;
      }else{
         isHav=true;
         return true;
      }
   }

   if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
   } 
      return true;
   }
</script>

HTML :

<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event)" />
0

alternatively you can use this..

function isNumber(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /^[0-9.,]+$/;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event)" />
Sarath
  • 2,318
  • 1
  • 12
  • 24
0

Follow the below code:

you need to change your function onkeypress event by adding this parameter just like below:

<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event, this)" />

and change your function as below:

function isNumber(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
            (charCode > 31 && (charCode < 48 || charCode > 57)))
        {
            return false;
        }

        return true;
    }

You can check below running code:

function isNumber(evt, element) {
        var charCode = (evt.which) ? evt.which : event.keyCode

        if ((charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
            (charCode > 31 && (charCode < 48 || charCode > 57)))
        {
            return false;
        }

        return true;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event, this)" />
-1

You can use type="number" attribute if it meet with your requirement

<input type="text" value="" id="tb1" name="tb1" onkeypress="return isNumber(event)" type="number" />

Or Use This

function isNumber(evt)
{
   var charCode = (evt.which) ? evt.which : evt.keyCode;
   if (charCode != 46 && charCode > 31 
     && (charCode < 48 || charCode > 57))
      return false;

   return true;
}
Sunil kumar
  • 761
  • 5
  • 15
  • This is an accurate script and allow only decimal or int value. I don't know why have u voted down this answer. – Sunil kumar Jun 15 '16 at 08:41