jake2891 Posted May 7, 2008 Share Posted May 7, 2008 Hi guys, I have an input box that im validating that the data is numeric. The problemis with the keypress event, when i click the backspace button i want it to do nothing. but at the moment it keeps bringing up my alert and its not working properly any help would be great. also it behaves differently in ie vs firefox. same code below. function validateNumeric(field,e) { var value = field.value; // get input value if(window.event) var key = window.event.keyCode; // ie else if(e) var key = e.which; // firefox if(isNaN(value)) { // check if value is not numeric alert('Please enter numerical data only.'); if(key == { // backspace button key.returnValue=false; } } } <input type="text" size="10" maxlength="10" onkeypress="validateNumeric(this);"> so basically it kind of working with validating that the data is numeric but when they click the backspace button to remove the text from the input box if they did so it keeps bringing up the alert message in the javascript. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 8, 2008 Share Posted May 8, 2008 how do you click a backspace button o.0 but anyway you should use a function that validates a number with the blur event <input onblur="checkNumber()" /> Quote Link to comment Share on other sites More sharing options...
bibby Posted May 11, 2008 Share Posted May 11, 2008 you should have lots of exceptions for key presses, like shift,ctrl,alt,esc, backspace, delete, insert, home, end, and arrow keys. here's a killer test page @ W3C : http://www.w3.org/2002/09/tests/keys.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.