﻿//初始化函数
//checkControlId:欲进行校验的控件ID
//tipControlId:错误提醒控件ID
function Validate(tipControlId){
	if(!document.getElementById(tipControlId)){
		alert("错误提醒控件不存在，请检查");
		return;
	}
	this.tip=document.getElementById(tipControlId);		//错误提醒控件
	this.tip.innerHTML="*";								//清除上一次提示信息
	this.check=null;									//进行校验的控件
	this.name=""		;								//出错后提示控件名称
	this.selected=true;									//出错后是否要选中内容
	this.lenDouble=false;								//是否检测双字节长度
	this.getFocus=true;									//是否取得焦点
	this.doTrim=true;
}

Validate.prototype.SetTipControl=function(tipControlId,oldTipText){
	if(!document.getElementById(tipControlId)){
		alert("错误提醒控件不存在，请检查");
		return;
	}
	this.tip=document.getElementById(tipControlId);		//错误提醒控件
	if(typeof(oldTipText)=="undefined"){
		this.tip.innerHTML="*";								//清除上一次提示信息
	}else{
		this.tip.innerHTML=oldTipText;
	}
}

Validate.prototype.SetControl=function(checkControlId,controlName){
	if(!document.getElementById(checkControlId)){
		this.tip.innerHTML="欲进行校验的控件不存在，请检查!";
		return;
	}
	this.check=document.getElementById(checkControlId);
	if(controlName != ""){
		this.name=controlName;
	}
}
Validate.prototype.SetSelected=function(selected){
	this.selected=selected;
}
Validate.prototype.SetTrim=function(dotrim){
	this.doTrim=dotrim;
}
Validate.prototype.SetFocus=function(isFocus){
	this.getFocus=isFocus;
}
Validate.prototype.SetLenDouble=function(lengthDouble){
	this.lenDouble=lengthDouble;
}

//检测字符是否为空
Validate.prototype.IsNull=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(checkValue==""){
		this.tip.innerHTML=this.name+"不能为空!";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return true;
	}else{
		return false;
	}
}

//检测字符串长度是否在minLen与maxLen之间
Validate.prototype.LenBetween=function(minLen,maxLen){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	var vlen=this.lenDouble?checkValue.len():checkValue.length;
	if(vlen < minLen || vlen > maxLen){
		this.tip.innerHTML=this.name+"长度必须介于"+minLen+"至"+maxLen+"个字符之间!";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//检测是否为数值(二位小数,0-999,999,999之间)只适合当前程序
Validate.prototype.IsMoney=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	var reg=/^(0(\.\d{1,2})?|[1-9]{1}\d{0,8}(\.\d{1,2})?)$/;
	if(!reg.test(checkValue)){
		this.tip.innerHTML=this.name+"必须是介0至999999999之间的数值,尾数允许两位小数!";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//检测是否为整数(0-999,999,999之间)只适合当前程序
Validate.prototype.IsInteger=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	var reg=/^0|[1-9]{1}\d{0,8}$/;
	if(!reg.test(checkValue)){
		this.tip.innerHTML=this.name+"必须是介0至999999999之间的整数!";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//检测是否为电话
Validate.prototype.IsPhone=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(!checkValue.isTelephone()){
		this.tip.innerHTML=this.name+"格式不正确，请检查";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//检测是否为手机
Validate.prototype.IsMobile=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(!checkValue.isMobilephone()){
		this.tip.innerHTML=this.name+"格式不正确，请检查";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//检测是否为电话或手机
Validate.prototype.IsTeleOrMobile=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(!(checkValue.isMobilephone() || checkValue.isTelephone())){
		this.tip.innerHTML=this.name+"格式不正确，请检查";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//是否电子邮件
Validate.prototype.IsEmail=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(!checkValue.isEmail()){
		this.tip.innerHTML=this.name+"格式不正确，请检查";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//是否QQ
Validate.prototype.IsQQ=function(){
	var checkValue=this.doTrim?this.check.value.trim():this.check.value;
	if(!checkValue.isQQ()){
		this.tip.innerHTML=this.name+"格式不正确，请检查";
		if(this.getFocus){
			this.check.focus();
		}
		if(this.selected){
			this.check.select();
		}
		return false;
	}else{
		return true;
	}
}
//去掉字符串中所有空字符
String.prototype.trimAll=function(){
    var reSpace=/\s/g;
    return this.replace(reSpace,"");
}
//去掉字符串中左边空字符
String.prototype.trimLeft=function(){
    var reSpace=/\s*(.*)/;
    return this.replace(reSpace,"$1")
}
//去掉字符串中右边空字符
String.prototype.trimRight=function(){
    var reSpace=/(.*?)\s+$/;
    return this.replace(reSpace,"$1")
}
//去掉字符串中左右两边空字符
String.prototype.trim=function(){
    var reSpace=/^\s*(.*?)\s*$/;
    return this.replace(reSpace,"$1")
}
//返回字符串长度，当是双字节时长度为2(例如一个汉字长度返回值为2)
String.prototype.len=function(){
    var stringLen=this.length;
    var reGb2312=/[^x00-xff]/g;        //[^x00-xff]是排除所有ASC值从0到255的所有字符，也就是排除所有单字节字符，剩下的都是非单字节字符(包括汉字,但不只是汉字）
    if(reGb2312.test(this)){
        stringLen=stringLen+this.match(reGb2312).length;
    }
    return stringLen;
}
//返回字符串长度是否在minLen与maxLen之间
String.prototype.between=function(minLen,maxLen){
    if(!minLen){
        minLen=0;
    }
    if(minLen>maxLen){
        temp=minLen;
        minLen=maxLen;
        maxLen=temp;
    }
    var stringLen=this.len();
    return stringLen>=minLen&&stringLen<=maxLen;
}
//是否国内电话号码，未加严格限制,分机用-分格
String.prototype.isTelephone=function(){
    var reTelephone=/^(010|02\d|0[3-9]\d{2,3})-?\d{7,8}(-\d{1,5})?$/;
    return reTelephone.test(this)
}
//是否国内手机,可加零
String.prototype.isMobilephone=function(){
    var reMobilephone=/^0?1(3\d{9}|5(8|9|0|1)\d{8}|8(8|9)\d{8})$/;
    return reMobilephone.test(this)
}
//是否email,前面包括范围(数字、字母、-、.)
String.prototype.isEmail=function(){
    var reEmail=/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    return reEmail.test(this)
}
String.prototype.isQQ=function(){
    var reEmail=/^[1-9]\d{3,9}$/
    return reEmail.test(this)
}
