Pergunta

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 ..

Foi útil?

Solução

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();
  }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top