1

I am trying to have my input only accept Numbers and the '.', it is working great but it doesn't allow for number pad number keys. I cant seem to find the exact answer online.

HTML

<input type="text" id="ItemTotal#i#" name="ItemTotal#i#" value="#qPriceAct#" onkeypress="return isNumeric(event)" onkeydown="return keyispressed(event);">

JavaScript

//prevent , and $ from being input
function keyispressed(e){
    var charval= String.fromCharCode(e.keyCode);
    if(isNaN(charval) && (e.which != 8 ) && (e.which != 190 )){
        return false;
    }
    return true;
}


//is input numeric
function isNumeric (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();
  }
}

Thanks for the help!

4
  • Have you tried pattern attribute? It is a regexp you put inside HTML and it checks validity on its own. Commented Sep 22, 2016 at 18:06
  • you mention it's used throughout your web app, is the class numbersOnly on each input that should only allow numbers? Commented Sep 22, 2016 at 18:25
  • Is "Num-lock" on? ... just in case Commented Sep 22, 2016 at 19:17
  • Ye snum lock is on, ive tried both ways Commented Sep 22, 2016 at 19:23

3 Answers 3

1

The best way I believe would be to add a class to all the inputs that only allow numbers. Then you can restrict any input that doesn't match the pattern or a number/decimal.

function numberVerfication(value) {
  var pattern=/^[0-9]*(\.)?[0-9]*$/;
  if (value.match(pattern) != null){
    return value
  }
  else {
    var p=/[0-9]*(\.)?[0-9]*/;
    return value.match(p)[0];
  }

}
$('.numbersOnly').keyup(function(e) {
  e.target.value = numberVerfication(e.target.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='numbersOnly' type="text" id="ItemTotal#i#" name="ItemTotal#i#" value="#qPriceAct#">

Sign up to request clarification or add additional context in comments.

Comments

0

Simple using REGEXP

HTML

<input type="text" class='numbersOnly' id="ItemTotali" name="ItemTotal#i" value="qPriceAct">

JQUERY

$('.numbersOnly').keyup(function (e) {
    this.value = this.value.replace(/[^0-9\.]/g, '');
});

JAVA SCRIPT

function numValidate(that){
  that.value = that.value.replace(/[^0-9\.]/g, '');  
}

<input type="text" onkeypress='numValidate(this)' onkeyup='numValidate(this)' class='numbersOnly' id="ItemTotali" name="ItemTotal#i" value="qPriceAct"/>

Demo

2 Comments

yes the problem here is that its used throughout my entire web app. it needs to be within the function and not class or id specific
@Charles Check it now.
0

function numberVerfication(value){
         return value.replace(/[^0-9.\-+]/g, '');
 }
$('.numbersOnly').keyup(function(e){
      e.target.value=numberVerfication(e.target.value);
});
      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.