//驗證身份證號碼
function IsIDCard(Obj)
{		
	var num = Obj.value;
	if(num != '')
	{
	
		var len = num.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})([A-Za-z0-9])$/);
		else 
		{
			alert("身份證號碼既不是15位也不是18位，請重新輸入");
			Obj.focus();
		}
		if(re)
		{
			var a = num.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) 
				{
					alert("身份證號碼中的出生日期不正確");
					Obj.focus();
				}
			}
			else
			{
				alert("身份證號碼格式不正確");
				Obj.focus();
			}
		}
	}
		
	
}

//驗證是否是整數，非字元
function IsNum(Obj, MinLength, MaxLength)
{
	
	var str = Obj.value;
	if(str != '')
	{
		var result = /^\d+$/.test(str);

		if(!result)
		{
			alert("您輸入的不是整數");
			Obj.focus();
		}
		else if(str.length < MinLength || str.length > MaxLength)
		{
			alert("您輸入的整數位數不在正常範圍");
			Obj.focus();
		}
	}
	
}

//驗證字串的長度
function IsLength(Obj, MinLength, MaxLength)
{
	var str = Obj.value;
	if(str != '')
	{		
		var str=new String(Obj.value);
		str = str.replace(/(^\s*)|(\s*$)/g, "");
		if (str.length==0) 
		{
			alert("沒有鍵入字串!");
			Obj.focus();
		}

		if(str.length < MinLength || str.length > MaxLength)
		{
			alert("字元位數不在"+MinLength+"到"+MaxLength+"之間，請重新修改");
			Obj.focus();
		}
	}
	
}

//驗證用戶名（只能用數字和字母）
function IsMemberName(Obj, MinLength, MaxLength)
{
	
	var str = Obj.value;
	if(str != '')
	{		
		var result = /^[a-z0-9\-]+$/.test(str);

		if(!result)
		{
			alert("您輸入的內容只能由小寫字母、數字和下劃線構成");
			Obj.focus();
		}
		if(str.length < MinLength || str.length > MaxLength)
		{
			alert("位數不在正常範圍");
			Obj.focus();
		}
	}
	
}

//驗證日期格式是否正確
function IsDate(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/) )		
		{
			alert("日期格式不正確，請用-或/分隔年月日");
			Obj.focus();
		}
	}
	
}

//驗證固定電話號碼是否正確
function IsFixPhone(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)/) )		
		{
			alert("固定電話號碼格式不正確");
			Obj.focus();
		}
	}
	
}

//驗證手機電話號碼是否正確
function IsMobilePhone(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/(^0{0,1}13[0-9]{9}$)/) )		
		{
			alert("移動電話號碼格式不正確");
			Obj.focus();
		}
	}
	
}

//驗證電話號碼是否正確
function IsPhone(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/) )		
		{
			alert("電話號碼格式不正確，您只能輸入一個電話號碼，固定電話或移動電話號碼均可");
			Obj.focus();
		}
	}
	
}

//驗證電子郵箱是否正確
function IsEmail(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/(^[A-Za-z0-9_\.\-]+@[A-Za-z0-9\-\.]+\.[A-Za-z]{2,4}$)/) )		
		{
			alert("電子郵箱格式不正確");
			Obj.focus();
		}
	}
	
}

//驗證兩字串是否一致
function IsSame(Obj1, Obj2)
{
	
	var str1 = Obj1.value;
	var str2 = Obj2.value;
	if(str1 != '' && str2 != '' && str1 != str2 )
	{
		alert("兩次輸入不一致");
		Obj2.focus();
		
	}
	
}

//檢查是否包含非法字元
function IsValidStr(Obj)
{
	var str = Obj.value;
	if(str != '')
	{	
		var str=new String(Obj.value);
		str = str.replace(/(^\s*)|(\s*$)/g, "");
		if (str.length==0) 
		{
			alert("沒有鍵入字串!");
			Obj.focus();
		}

		var poisions=new Array("/","\\",":",";","*","?","\"","<",">","|","@","$","%","^","~","`");	
		var x_cut;
		var flag;
		for(var j=0 ;j<str.length;j++)
		{  
			x_cut=str.substr(j,1);
			//alert(str+"的第"+j+"個字元為:"+x_cut)
			for(var i=0 ;i<poisions.length;i++)
			{
				if (x_cut==poisions[i])
				{
					alert("字串中不能含有如下字元:\n"+poisions);
					flag = 1;
					break;
				}
			}

			if(flag) break;
		}
		
		if(flag) Obj.focus();
	}   
}

//驗證網址是否正確
function IsURL(Obj)
{
	
	var str = Obj.value;
	if(str != '')
	{	
		if(null == str.match(/(^http:\/\/[A-Za-z0-9\-\.]+\.[A-Za-z]{2,4}$)/) )		
		{
			alert("網址格式不正確，請檢查是否以http://開頭");
			Obj.focus();
		}
	}
	
}
