现在的位置: 首页 > 综合 > 正文

常用的表单验证及一些字符串转义函数--JS

2013年09月07日 ⁄ 综合 ⁄ 共 3037字 ⁄ 字号 评论关闭

// 是否为空,非空返回真,不非为空返回假
function isBlank(str) {
var blankFlag = true;
   
if (str.length == 0) return true;
   
for (var i = 0; i < str.length; i++) {
       
if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
            blankFlag
= false;
           
break;
        }
    }
   
return blankFlag;
}
function checkNotNull(theField, fieldName) {
   
if(isBlank(theField.value)){
        alert(fieldName
+ "不可为空!");
               
if(theField.type!="hidden"){
          theField.focus();
                }
       
return false;
    }
   
return true;
}

// 是否为数字
function checkNumber(theField, fieldName) {
    
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
    
if(theField.value.trim() == "") return true;
    
if (!pattern.test(theField.value.trim())) {
         alert(fieldName
+ "必须为合法数字");
         theField.focus();
         theField.select();
        
return false;
     }

    return true;
}

function isNumber(str) {
 
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
 
if(str.trim() == "") return false;
 
if (!pattern.test(str.trim())) return false;
 
return true;
}

// 是否为指定范围数字
function checkNumberRange(theField, fieldName, min, max) {
   
if(theField.value.trim() == "") return true;
   
if (!checkNumber(theField, fieldName)) return false;

    if ((min != "") && (theField.value.trim() < min)) {
        alert(fieldName
+ "不可小于" + min + "");
        theField.focus();
        theField.select();
       
return false;
    }

    if ((max != "") && (theField.value.trim() > max)) {
        alert(fieldName
+ "不可超过" + max + "");
        theField.focus();
        theField.select();
       
return false;
    }

    return true;
}

function isNumberRange(str, min, max) {
if(str == "") return false;
   
if (!isNumber(str)) return false;

    if ((min != "") && (str < min)) {
       
return false;
    }

    if ((max != "") && (str > max)) {
       
return false;
    }

    return true;
}

// 是否为整数
function checkInteger(theField, fieldName) {
   
var pattern = /^(/d|(-/d))/d*$/;

    if(theField.value.trim() == "") return true;
   
if (!pattern.test(theField.value.trim())) {
        alert(fieldName
+ "必须为整数!");
        theField.focus();
        theField.select();
       
return false;
    }

    return true;
}

function isInteger(str) {
   
var pattern = /^(/d|(-/d))/d*$/;

    if(str.trim() == "") return false;
   
if (!pattern.test(str.trim())) {
       
return false;
    }
   
return true;
}

// 是否为指定范围内整数
function checkIntegerRange(theField, fieldName, min, max) {
   
if(theField.value.trim() == "") return true;
   
if (!checkInteger(theField, fieldName)) return false;

    if ((min != "") && (theField.value.trim() < min)) {
        alert(fieldName
+ "不可小于" + min + "");
        theField.focus();
        theField.select();
       
return false;
    }

    if ((max != "") && (theField.value.trim() > max)) {
        alert(fieldName
+ "不可超过" + max + "");
        theField.focus();
        theField.select();
       
return false;
    }

    return true;
}

function isIntegerRange(str, min, max) {
   
if(str == "") return false;
   
if (!isInteger(str)) return false;

    if ((min != "") && (str < min)) {
       
return false;
    }

    if ((max != "") && (str > max)) {
       
return false;
    }

    return true;
}

// 是否为正数
function checkPositiveNumber(theField, fieldName) {
   
if(theField.value == "") return true;
   
if (theField.value.charAt(0) == '-') {
        alert(fieldName
+ "必须为正数!");
        theField.focus();
       
return false;
    }

    return true;
}

抱歉!评论已关闭.