本文转自:http://forums.asp.net/t/1211724.aspx?OnKeyDown+Numeric+Validator+CLIENT+SIDE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Numbers Only Textbox by Josh Stodola</title> <script type="text/javascript"> function numbersOnly(e) { var evt = (e) ? e : window.event; var key = (evt.keyCode) ? evt.keyCode : evt.which; if(key != null) { key = parseInt(key, 10); if((key < 48 || key > 57) && (key < 96 || key > 105)) { if(!isUserFriendlyChar(key)) return false; } else { if(evt.shiftKey) return false; } } return true; } function isUserFriendlyChar(val) { // Backspace, Tab, Enter, Insert, and Delete if(val == 8 || val == 9 || val == 13 || val == 45 || val == 46) return true; // Ctrl, Alt, CapsLock, Home, End, and Arrows if((val > 16 && val < 21) || (val > 34 && val < 41)) return true; // The rest return false;