
var formcheck = function(arr){
	this.lenl			=	0;									//小于的长度范围
	this.lenr			=	0;									//大于的长度范围
	this.obj			=	null;								//文本对象
	this.pwd1			=	null;								//密码框1
	this.pwd2			=	null;								//密码框2
	this.emailreg		=	/^[\-\.\w]+@[\.\-\w]+(\.\w+)+$/;	//email正则表达式
	this.mobilereg		=	/^0?1[0-9]{10}$/;					//手机号码正则表达式
	this.chinesereg		=	/[^\x00-\xff]/g;					//判断中文字符
	this.msg			=	"";									//提示信息
	this.init();												//初始化函数
};

formcheck.prototype = {
	//判断提示信息指定是否有误
	ckmsg:function(){
		if (typeof this.msg != "string"){
			alert("错误：提示内容指定有误！");
			return false;
		}
		else if (this.msg == ""){
			alert("错误：未指定提示内容！");
			return false;
		}
		else {
			return true;
		}
	},
	//判断文本对象指定是否有误
	ckobj:function(myobj){
		if (myobj == null){
			alert("错误：未指定对象！");
			return false;
		}
		else if (myobj.length == 0){
			alert("错误：指定对象不存在！");
			return false;
		}
		else {
			return true;
		}
	},
	//判断文本对象和提示信息的传递参数是否有误
	ck:function(){
		if (!this.ckobj(this.obj)) return false;
		if (!this.ckmsg()) return false;
		if (this.obj.attr("type") != "password"){
			this.obj.val(jQuery.trim(this.obj.val()));
		}
		return true;
	},
	//判断数值长度指定是否有误
	ckl:function(){
		if (!this.ck()) return false;
		if ( typeof this.lenl != "number" || typeof this.lenr != "number" ){
			alert("错误：限定的字符长度指定有误！");
			return false;
		}
		return true;
	},
	//判断两个密码框指定是否有误
	ckp:function(){
		if (!this.ckobj(this.pwd1)) return false;
		if (!this.ckobj(this.pwd2)) return false;
		if (!this.ckmsg()) return false;
		return true;
	},
	//弹出提示信息
	ckalert:function(){
		alert(this.msg);
		if (this.obj != null){
			this.obj.focus();
			this.obj.select();
			return;
		}
		if (this.pwd1 != null){
			this.pwd1.focus();
			this.pwd1.select();
			return;
		}
	},
	
	//将中文转化为双字符
	chinese:function(){
		return this.obj.val().replace(this.chinesereg, "**").length;
	},
	//判断obj对象的内容是否为空
	cknull:function(obj, msg, str){
		this.init( {obj:obj, msg:msg} );
		if (!this.ck()) return false;
		str = typeof str == "undefined" ? "" : str;
		if (this.obj.val() == str || this.obj.val() == ""){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj对象的内容不能小于L
	cklenl:function(obj, l, msg){
		this.init({obj:obj, lenl:l, msg:msg});
		if (!this.ckl()) return false;
		if (this.chinese() < this.lenl){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj对象的内容不能大于R
	cklenr:function(obj, r, msg){
		this.init({obj:obj, lenr:r, msg:msg});
		if (!this.ckl()) return false;
		if (this.chinese() > this.lenr){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判pwd1的内容与pwd2的内容是否相同
	ckpwd:function(pwd1, pwd2, msg){
		this.init({pwd1:pwd1, pwd2:pwd2, msg:msg});
		if (!this.ckp()) return false;
		if (this.pwd1.val() != this.pwd2.val()){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj的内容是否符合Email格式
	ckemail:function(obj, msg){
		this.init({obj:obj, msg:msg});
		if (!this.ck()) return false;
		if (!this.emailreg.test(this.obj.val())){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj的内容是否为数字
	cknum:function(obj, msg){
		this.init({obj:obj, msg:msg});
		if (!this.ck()) return false;
		if (this.obj.val() != Number(this.obj.val())){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj的内容是否在L与R之间
	ckrange:function(obj, l, r, msg){
		if (!this.cknum(obj, msg)) return false;
		this.init({obj:obj, lenl:l, lenr:r, msg:msg});
		if (!this.ckl()) return false;
		if (l > r){
			this.lenl = r;
			this.lenr = l;
		}
		if (Number(this.obj.val()) < this.lenl || Number(this.obj.val()) > this.lenr){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//判断obj的内容是否符合手机号码的格式
	ckmobile:function(obj, msg){
		this.init({obj:obj, msg:msg});
		if (!this.ck()) return false;
		if (!this.mobilereg.test(this.obj.val())){
			this.ckalert();
			return false;
		} else {
			return true;
		}
	},
	//初始化函数
	init:function(arr){
		if (arr != null){
			if (typeof arr.obj != "undefined") this.obj = arr.obj; else this.obj = null;
			if (typeof arr.msg != "undefined") this.msg = arr.msg; else this.msg = "";
			if (typeof arr.lenl != "undefined") this.lenl = arr.lenl; else this.lenl = 0;
			if (typeof arr.lenr != "undefined") this.lenr = arr.lenr; else this.lenr = 0;
			if (typeof arr.pwd1 != "undefined") this.pwd1 = arr.pwd1; else this.pwd1 = null;
			if (typeof arr.pwd2 != "undefined") this.pwd2 = arr.pwd2; else this.pwd2 = null;
		}
	}
};

//创建名为myck的表单验证对象
var myck = new formcheck();

