日期:2014-05-16  浏览次数:20429 次

js验证总结

js文件

?

//测试输入框的值是否整数
function checkNum(inputObj) {
    var regex = /^/d*$/;
    return regex.test(inputObj.value);
}


//测试输入框的值的个数
function checkLength(inputObj, maxLength) {
    return inputObj.value.length <= maxLength;
}


//将字符串的首尾空格全部去掉
String.prototype.Trim = function() {
    return this.replace(/(^/s*)|(/s*$)/g, "");
}


//测试输入框是否空值.value
function checkInputValueIsEmpty(inputObj) {
    var s = inputObj.value.Trim();
    if (s == null || s == "") {
        return false;
    }
    else {
        return true;
    }
}


//判断一个控件的值是否为空
function checkValueIsEmpty(inputValue) {
    var strValue = inputValue.Trim();
    if (strValue == null || strValue == "") {
        return true;
    }
    else {
        return false;
    }
}


//验证最大长度指定的正小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
function checkNumberLength(inputObj, beforeLength, afterLength) {
    if (inputObj.value.indexOf(".") >= 0) {
        var regex = new RegExp("^//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
        return regex.test(inputObj.value);
    }
    else {
        var regex = new RegExp("^//d{0," + beforeLength + "}$");
        return regex.test(inputObj.value);
    }
}


//验证最大长度指定的正负小数,inputObj为input对象,beforeLength为小数点前面的位数个数,afterLength为小数点后面的位数个数
function checkAllNumberLength(inputObj, beforeLength, afterLength) {
    if (inputObj.value.indexOf(".") >= 0) {
        var regex = new RegExp("^-?//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
        return regex.test(inputObj.value);
    }
    else {
        var regex = new RegExp("^-?//d{0," + beforeLength + "}$");
        return regex.test(inputObj.value);
    }
}

//验证最大长度指定的正负小数value
function checkAllNumberLengthByValue(inputValue, beforeLength, afterLength) {
    if (inputValue.indexOf(".") >= 0) {
        var regex = new RegExp("^-?//d{0," + beforeLength + "}[.]?//d{0," + afterLength + "}$");
        return regex.test(inputValue);
    }
    else {
        var regex = new RegExp("^-?//d{0," + beforeLength + "}$");
        return regex.test(inputValue);
    }
}


//测试输入框的值是否带小数
function checkNumber(inputObj) {
    var regex = /^([0-9]*)([.]?)([0-9]*)$/;
    return regex.test(inputObj.value);
}


//测试输入框的值是否小数
function checkAllNumber(inputObj) {
    var regex = /^-?([0-9]*)([.]?)([0-9]*)$/;
    return regex.test(inputObj.value);
}


//测试身份证验证
function checkIDnumber(inputObj) {
    var regex = /(^/d{15}$)|(^/d{17}([0-9]|X)$)/;
    return regex.test(inputObj.value);
}


//测试身份证验证
function checkemail(inputObj) {
    var regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    return regex.test(inputObj.value);
}


function KeyPress(inputObj) {//只允许录入数据字符 0-9 和小数点
    var txtval = inputObj.value;
    var key = event.keyCode;
    if ((key < 48 || key > 57) && key != 46) {
        event.returnValue = false;
    }
    else if (key == 46) {
        if (txtval.indexOf(".") != -1 || txtval.length == 0)
            event.returnValue = false;
    }
}


function KeyPressAllNumber(inputObj) {//只允许录入所有小数
    var txtval = inputObj.value;

    var key = event.keyCode;
    if ((key < 48 || key > 57) && key != 46 && key != 45) {
        event.returnValue = false;
    }
    else if (key == 46) {
        if (txtval.indexOf(".") != -1 || txtval.length == 0)
            event.returnValue = false;
    }
    else if (key == 45) {
        if (txtval.indexOf("-") != -1)
            event.returnValue = false;
    }
}


//输入整数
function KeyPressNum(objTR) {//只允许录入数据字符 0-9
    //var objTR = element.document.activeElement;        
    var txtval = objTR.value;

    var key = event.keyCode;
    if (key < 48 || key > 57) {
        event.keyCode = 0;
    }
}


function checkInputValueIsEmptyOrOverLength(inputObj, inputLength, inputField) {
    if (!checkInputValueIsEmpty