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.charCodeAt(i) > 128)
			strlen += 2;
		else 
			strlen++;
       	 	s += str.charAt(i);
		if(strlen >= len) 
        	return s + "...";
	}
	return s;
}
/**
 *判断浏览器类型
 */
function getOs() { 
   var OsObject = ""; 
   if(navigator.userAgent.indexOf("MSIE")>0) { 
        return "MSIE"; 
   } 
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
        return "Firefox"; 
   } 
} 

// 获取元素的绝对坐标
function gE(x){return document.getElementById(x);}
function gETop(x){var t=0;while(x){t+=x.offsetTop;x=x.offsetParent;}return t;}
function gEBottom(x){return (x.offsetHeight+gETop(x));}
function gELeft(x){var l=0;while(x){l+=x.offsetLeft;x=x.offsetParent;}return l;}
function gERight(x){return (x.offsetWidth+gELeft(x));}

// 获取两个日期的间隔天数
function getDateDiff(startDate, endDate) { 
	var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime(); 
	var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime(); 
	var dates = Math.abs((startTime - endTime))/(1000*60*60*24); 
	return dates; 
}
var specialDateMap = {
	"2012-02-06":"元宵节",
	"2012-02-07":"十六",
	"2012-02-14":"情人节",
	"2012-04-04":"清明节",
	"2012-05-01":"劳动节",
	"2012-06-01":"儿童节",
	"2012-06-23":"端午节",
	"2012-08-01":"建军节",
	"2012-09-10":"教师节",
	"2012-09-30":"中秋节",
	"2012-10-01":"国庆节",
	"2012-12-25":"圣诞节",
	"2013-01-01":"元旦节",
	"2013-02-10":"春节",
	"2013-02-24":"元宵节",
	"2013-04-04":"清明节",
	"2013-05-01":"劳动节",
	"2013-06-01":"儿童节",
	"2013-06-12":"端午节",
	"2013-09-10":"教师节",
	"2013-09-19":"中秋节",
	"2013-10-01":"国庆节",
	"2013-12-25":"圣诞节",
	"2014-01-01":"元旦节"
};
var weekArr = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
function getSpecialDateTip(strDate) {
	var nowdt = new Date();
    var today = nowdt.getFullYear()+"-"+(nowdt.getMonth()+1)+"-"+ nowdt.getDate();
    var d = getDateDiff(today, strDate);
    if(d==0) {return '今天'; }
    else if(d==1) {return '明天';}
    else if(d==2) {return '后天';}
	if(specialDateMap[strDate]) {
		return specialDateMap[strDate];
	}
	else {
		var date = new Date(Date.parse(strDate.replace(/-/g, "/")));
		return weekArr[date.getDay()];
	}
}
 

 

你可能感兴趣的:(js)