1

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

1
  • var regex = /^[0-9_\b]+$/; - numbers only + backspace Commented May 6, 2013 at 7:07

1 Answer 1

1

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();
  }
}
Sign up to request clarification or add additional context in comments.

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.