Question

I work on input validation (numeric input from kaybord only). Which works fine but the problem is that i can not use backspace button. Delete button works well.

My code:

function validate(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();
  }
}

I know i should adjust regex but im not so sure how ... need help on this one

Thanks a lot in advance ..

Was it helpful?

Solution

Its all working.. numeric input + working bacspace

    function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  **var regex = /^[0-9_\b]+$/;**
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top