js判断是否整数,重程的写法,确定不能判断形如1.2的小数

<input id="e_num" onfocus="txtBoxfocus(this);" onchange="intCheck(this);" type="text" value="0" />

//=====================================校验====================================
//===================================输入格式校验==================================
var initVal = null;
// textBox onfocus
function txtBoxfocus(txtBox) {
    initVal = txtBox.value;
};

// int input check
function intCheck(txtBox) {
    if (!isPositiveInt(txtBox.value) || Number(txtBox.value) <= 0) {
        alert('请输入整数!');
        resetTxtBoxVal(txtBox);
        return false;
    }
    return true;
};

String.prototype.IsPositiveInt=function()
{
    var str=this;
    var result = str.match(/^[1-9]\d*|0$/)
    return result==null? false : true;
}

function resetTxtBoxVal(txtBox) {
    txtBox.value = initVal;
};

posted on 2011-08-31 17:36  冰危节奏  阅读(384)  评论(0)    收藏  举报

导航