1

I want to copy paste numeric values in this field, can we achieve this keeping onkeypress as it is?

HTML:

 <input type="text" onkeypress="return allowNumOnly(event);">

Javascript:

  function allowNumOnly(evt)
  {
     var charCode = (evt.which) ? evt.which : evt.keyCode
     console.log(charCode);
     if(charCode==36 || charCode==46 || charCode==37 || charCode==39)
        return true;
     if (charCode > 31 && (charCode < 48 || charCode > 57))
       return false;

     return true;
  }
chris97ong
  • 6,267
  • 6
  • 28
  • 48
Mr bond
  • 127
  • 2
  • 10
  • 1
    I believe the only way to do this would be to respond to the onchange event and check if the content was a number or not. The keyPress, keyUp and keyDown events will only return ctrl and v not the actual numbers being pasted. – MarshallOfSound Jun 12 '14 at 03:48
  • See here: http://stackoverflow.com/questions/686995/catch-paste-input – jasonscript Jun 12 '14 at 05:12

0 Answers0