-2

嗨,我没有正确收到我的警报消息,我的代码是:

function isNumberKey( event ) {
    var charCode = (event.which)? event.which: event.keyCode;
    var ctrl = event.ctrlKey;
    var shift1=event.shiftKey;
    var zipcode=document.getElementById(zipCode).value;

    if ( ctrl ) {
       return true;
    }
    if ( ( charCode >= 95 && charCode <= 105) || (charCode >= 106 && charCode <= 123 ) ) {
       return true;
    }
    if ( charCode == 36 || charCode == 35 || charCode == 45 || charCode == 46 || charCode == 144 || charCode == 145 ) {
       return true;
    }
    if ( charCode >= 106 && charCode <= 123 ) {
       return true;
    }
    if ( ( shift1 ) || charCode > 41 && (charCode < 48 || charCode > 57 ) ) {
   alert("The ZIP Code entered is invalid. Please enter a 5 digit ZIP Code.");
       return true;
    }   
} 

有人可以帮忙吗?

4

1 回答 1

0

您可能错过了添加引号

var zipcode=document.getElementById('zipCode').value;  //MISSED '

更新:

而是简化您的代码,如下所示

<input type='text' maxlength='5' id='zipCode' onkeypress='isNumberKey(this)' />

JS:

function isNumberKey(event) {
    event.value = event.value.replace(/[^\d]/ig, "");
}

签入JSFiddle

于 2013-09-19T07:43:15.520 回答