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

js的验证
// 格式化数字,保持小数点位数
if(!Number.prototype.toFixed) {
	Number.prototype.toFixed=function(num) {
		with(Math)return   round(this.valueOf()*pow(10,num))/pow(10,num);   
	};
}
// 身份证号码验证
function isIdCardNo(num) {
	
	var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
	//var error;
	var varArray = new Array();
	var intValue;
	var lngProduct = 0;
	var intCheckDigit;
	var intStrLen = num.length;
	var idNumber = num;
		 // 输入身份证号码长度判断
     if ((intStrLen != 15) && (intStrLen != 18)) {
         return false;
     }
     // 数字位判断
     for(i=0;i<intStrLen;i++) {
         varArray[i] = idNumber.charAt(i);
         if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
             return false;
         } else if (i < 17) {
             varArray[i] = varArray[i]*factorArr[i];
         }
     }
     if (intStrLen == 18) {
         // 出生日期判断
         var date8 = idNumber.substring(6,14);
         if (isDate8(date8) == false) {
             return false;
         }
         // 计算校验位
         for(i=0;i<17;i++) {
             lngProduct = lngProduct + varArray[i];
         }
         intCheckDigit = 12 - lngProduct % 11;
         switch (intCheckDigit) {
             case 10:
                 intCheckDigit = 'X';
                 break;
             case 11:
                 intCheckDigit = 0;
                 break;
             case 12:
                 intCheckDigit = 1;
                 break;
         }
         // 判断校验位
         if (varArray[17].toUpperCase() != intCheckDigit) {
             return false;
         }
     }
     else{
		 // 出生日期判断
         var date6 = idNumber.substring(6,12);
         if (isDate6(date6) == false) {
             return false;
         }
     }
     return true;
}

// email验证
function isEmail(email) {
	if (email=="") return true;
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}
// 手机号码验证
function isMobilePhone(mobilePhone) {
	if (mobilePhone=="") return true;
	return (mobilePhone.length == 11 && /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(mobilePhone));
}
// YYYYMM日期格式验证
function isDate6(sDate) {
	return isDate8("19"+sDate);
} 

// YYYYMMDD日期格式验证
function isDate8(sDate) {
	if(!/^[0-9]{8}$/.test(sDate)) {
		return false;
	}
	var year, month, day;
	year = sDate.substring(0, 4);
	month = sDate.substring(4, 6);
	day = sDate.substring(6, 8);

	var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31];

	if (year < 1900 || year > 2100) return false;
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
	if (month < 1 || month > 12) return false;
	if (day < 1 || day > iaMonthDays[month - 1]) return false;
	return true;
} 


/**
 * 判断字符串的长度,超出部分用省略号表示
 */
function strlong(str,num){
	var showstr = "";
	if(str.length > num){
		showstr = str.substring(0,num)+"...";
	}
	else{
		showstr = str;
	}
	return showstr;
}
/**
 * 判断字符串的长度,超出部分用省略号表示
 */ 
function SetString(str,len){
 	var strlen = 0; 
 	var s = "";
	for(var i = 0;i < str.length;i++){
		if(str.char