我试图找出为什么我的javascript代码不限制数字输入。这是下面的代码:
(function(){
if(window.addEventListener) window.addEventListener("load",init,false);
else if(window.attachEvent) window.attachEvent("onload",init);
function init() {
var keytest = document.getElementById("keytest");
function numericinput(event) {
var e = event || window.event;
var key = e.charcode || e.keyCode;
var regex =/^\d+\.?\d{4}$/;
key = String.fromCharCode(key);
if (!regex.test(key)) {
alert("something");
//if(e.preventDefault) e.preventDefault();
//if(e.returnValue) e.returnValue = false;
//return false;
}
else { //return true;
alert("something else");
}
}
if(keytest.addEventListener) keytest.addEventListener("keypress",numericinput,false);
else {
keytest.onkeypress = numericinput();
}
}
})();