js校验 check

/**
 * 验证提交的表单
 */
function checkForm(checkGroup){
	if(typeof(checkGroup) == "undefined"){
		checkGroup = "";
	}
	var _val = 0;
	for(var i = 0 ; i < _checkFormList.length ; i++){
		var checkItem = _checkFormList[i];
		if(checkGroup == checkItem.group){
			var flag = true;
			var value = "";
			
			if(!(getobj(checkItem.id))){
				continue;
			}
			if(checkItem.controlType == "checkBox" || checkItem.controlType == "radio"){
				var _obj = document.getElementsByName(checkItem.id);
				var objarray=_obj.length;
				var chestr="";
				for (var k=0;k<objarray;k++){
					if(_obj[k].checked == true)
					{
						value += _obj[k].value;
					}
				}
			}else{
				value = getobj(checkItem.id).value;
			}
			
			
			if(checkItem.isNULL == true){
				flag = checkIsNULL(value);
			}
			if(flag&&value!=""){
				flag = checkItem.fun(value);
			}
			if(flag == false){
				if(checkItem.controlType == "checkBox" || checkItem.controlType == "radio"){
					var _obj = document.getElementsByName(checkItem.id);
					var objarray=_obj.length;
					var chestr="";
					for (var k=0;k<objarray;k++){
						_obj[k].nextSibling.style.backgroundColor="red";
					}
				}else{
					//getobj(checkItem.id).style.border = "red 1px solid";
					getobj(checkItem.id).style.background = "red";
				}
			
				_val++;
			}else{
				if(checkItem.controlType == "checkBox" || checkItem.controlType == "radio"){
					var _obj = document.getElementsByName(checkItem.id);
					var objarray=_obj.length;
					var chestr="";
					for (var k=0;k<objarray;k++){
						_obj[k].nextSibling.style.backgroundColor="";
					}
				}else{
					getobj(checkItem.id).style.backgroundColor = "";
				}
			}
		}
	}
	return _val>0?false:true;
}

function Mout(o){
	if(!o){
		return;
	}
	timer=setTimeout(function(){o.style.display="none";},100);
	swtemp=0;
}

function hiddiv(e,inputid){
    e=e||window.event;
    ev=e.target||e.srcElement;
    v=ev.innerText||ev.textContent;
    getobj(inputid).value=v;
    getobj(inputid+"mydiv").style.display="none";
}
	
function getobj(o){//获取对象
    return document.getElementById(o);
}

function createDiv(_parent,_element,_id,_css){//创建层
    var newObj = document.createElement(_element);
    if(_id && _id!=""){
    	newObj.id=_id;
    }
    if(_css && _css!=""){
        newObj.setAttribute("style",_css);
        newObj.style.cssText = _css;
    }
    if(_parent && _parent!=""){
        var theObj=getobj(_parent);
        var parent = theObj.parentNode;
        if(parent.lastChild == theObj){
           theObj.appendChild(newObj);
        } 
        else{
           theObj.insertBefore(newObj, theObj.nextSibling);
        }
    }
    else{
    	document.body.appendChild(newObj);
    }
}


function offsetTop( elements ){ 
	var top = elements.offsetTop; 
	var parent = elements.offsetParent; 
	while( parent != null ){ 
		top += parent.offsetTop; 
		parent = parent.offsetParent; 
	}; 
	
	return top-$(document).scrollTop(); 
}; 
function offsetLeft( elements ){ 
	var left = elements.offsetLeft; 
	var parent = elements.offsetParent; 
	while( parent != null ){ 
		left += parent.offsetLeft; 
		parent = parent.offsetParent; 
	}; 
	return left; 
}; 

/** 验证函数 **/

//验证车牌号码
function checkCarNo(value){
	if((/^[VKHBSLJNGCZE\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9\u8b66\u5b66\u6302]{1}$/g).test(value)){
		return true;
	}else{
		return false;
	}
}

//验证是否为空
function checkIsNULL(value){
	if(value == "" || value == null || typeof(value) == "undefined"){
		return false;
	}else{
		return true;
	}
}

//验证IP
function checkIP(value){
	var ip = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
	return ip.test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256); 
}

//验证邮箱
function checkEmail(value){
	var myreg = /^([a-zA-Z0-9_-]+[_|\_|\.]?)*[a-zA-Z0-9_-]+@([a-zA-Z0-9_-]+[_|\_|\.]?)*[a-zA-Z0-9_-]+\.[a-zA-Z]{2,3}$/;
	return myreg.test(value);
}

//验证家庭电话
function checkTel(value){
	var partten = /^0(([1,2]\d)|([3-9]\d{2}))\d{7,8}$/;
	return partten.test(value);
}

//验证手机电话
function checkPhone(value){
	var length = value.length;    
	return length == 11 && /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/.test(value);    
}
//身份证验证
function checkCard(value){  
    var len = value.length, re;  
    if (len == 15)  
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);  
    else if (len == 18)  
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);  
    else{   
        return false;  
    }  
    var a = value.match(re);  
    if (a != null){  
        if (len==15){  
            var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);  
            var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];  
        }else{  
            var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);  
            var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];  
        }  
        if (!B){ 
            return false;  
        }  
    }  
  
    return true;  
}

/**
 * 以下方法未验证 有问题需要进行修改
 * 
 * @returns
 */
//验证整数和小数点	
function checkDouble(value){
	var decimal = /^[1-9]\d*|^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$/;
	return (decimal.test(value));
}

//验证值小数位数不能超过两位
function checkDecimal(value){
	var decimal = /^-?\d+(\.\d{1,2})?$/;
	return (decimal.test(value));
}

//检查是否数字
function checkNum(s)
{
	var reg = new RegExp("^-?[0-9]*$");
	if(reg.test(s))
	{
	   return true;
	}
	return false;
}
//检查是否整数
function checkInt(s)
{
	var reg = new RegExp("^[0-9]*$");
	if(reg.test(s))
	{
	   return true;
	}
	return false;
}

//检查是否为正整数 
function checkUnsignedInteger(s) 
{ 
	var reg = new RegExp("^[0-9]*$");
	if(reg.test(s))
	{
		if(s>0){
			return true;
		}else{
			return false;
		}
	   
	}
	return false;
}

//验证短日期
function checkDate(str) 
{ 
   var a = str.match(/^(\d{1,4})(-)(\d{1,2})\2(\d{1,2})$/);  
   if(a==null)return false;  
   if (a[3]>12 ||a[4]>31) //需要加入特殊月份验证
   { 
       return false;
   } 
   return true;
}
//验证长时间
function checkDateTime(str) 
{ 
  // var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;  
   var reg = /^(\d{1,4})(-)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;  
   var a = str.match(reg);  
   if(a==null)return false;
   if (a[3]>12 ||a[4]>31 || a[5]>23 || a[6]>59 || a[6]>59) //需要加入特殊月份验证
   { 
       return false;
   } 
   return true;
} 

//验证时间(10:57:10) 
function checkTime(str) 
{ 
   var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/); 
   if (a == null) {return false;} 
   if (a[1]>23 || a[3]>59 || a[4]>59) 
   { 
       return false;
   } 
   return true; 
}
//比较两个日期大小 
function compareDate(d1,d2) 
{ 
    return ((new Date(d1.replace(/-/g,"\/"))) < (new Date(d2.replace(/-/g,"\/")))); 
}

//特殊字符验证
function checkString(value){
	 return /^[\Α-\¥\w]+$/.test(value);
}

function checkTelLength(value){
	if(value.length<7){
		return false;
	}
	 return true; 
	
}

function changeStrNull(str){
	if(typeof(str) == "undefined" || str == ""){
		str = "";
	}
	return str;
}

//非字母特殊符号
function clearNoNum(obj,maxValue){
	if(obj.value.length>1){
		if(0 == obj.value.substr(0,1) && "." != obj.value.charAt(1)){
			obj.value = obj.value.replace(obj.value,"");
		}
	}
	//先把非数字的都替换掉,除了数字	
	obj.value = obj.value.replace(/[^\d.]/g,"");		
	//必须保证第一个为数字而不是.		
	obj.value = obj.value.replace(/^\./g,"");		
	//保证只有出现一个.而没有多个.		
	obj.value = obj.value.replace(/\.{2,}/g,".");		
	//保证.只出现一次,而不能出现两次以上		
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	if(parseFloat(obj.value)>maxValue)
		obj.value=obj.value.substring(0,obj.value.length-1);
}

//校验只是数字
function checkNoNum(obj){	
	if(obj.value.length>1){
		if(0 == obj.value.substr(0,1) && "." != obj.value.charAt(1)){
			obj.value = obj.value.replace(obj.value,"");
		}
	}
	obj.value = obj.value.replace(/[^\d]/g,"");	
	obj.value = obj.value.replace(/[^\d]/g,"");		
	obj.value = obj.value.replace(/^\./g,"");			
	obj.value = obj.value.replace(/\.{2,}/g,".");		
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

}

//校验只是数字
function checkTel(obj){	
	obj.value = obj.value.replace(/[^\d]/g,"");		
	obj.value = obj.value.replace(/^\./g,"");			
	obj.value = obj.value.replace(/\.{2,}/g,".");			
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}

function checkMoneyFormat(val){
//	var reg = /^(([1-9]+)|([0-9]+\.[0-9]{0,5}))$/;
	var reg = /^(\d+\.\d{0,5}|\d+)$/;
    var isMoneyFormatRight = reg.test(val);
    return isMoneyFormatRight;
}

function changeTwoDecimal(floatvar){
	var f_x = Math.round(floatvar*100)/100;
	return f_x;
}

你可能感兴趣的:(JavaScript)