﻿/*数据验证方法、字符串处理方法*/
//验证email地址
String.prototype.isEmail = function()
{
	return ( this.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != null );
}
//截除字符串前后空格
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

//将"&"转换为"&amp;","<"号转换为"&lt;"，">"转换为"&gt;"
String.prototype.transform = function()
{
	return this.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}
						
//是否为空（包括空格）
String.prototype.isEmpty = function()
{
	return ( this.match(/^\s*$/) != null );
}
							
//是否为数值(包括正负整数、小数和0)
String.prototype.isNumeric = function()
{
	return ( this.isInteger() || this.isDecimal() );
}

//是否为整数(包括负数和0)
String.prototype.isInteger = function()
{
	return ( this.match( /^\-?\d+$/ ) != null );
}

//是否为非负整数(不包括-0)
String.prototype.isPlusInteger = function()
{
	return ( this.match( /^\d+$/ ) != null );
}

//是否为小数(包括负数和0.00...)
String.prototype.isDecimal = function()
{
	return ( this.match( /^((\-\d+)|(\d*))\.\d+$/ ) != null );
}

//是否为非负小数(不包括0.00...)
String.prototype.isPlusDecimal = function()
{
	return ( this.match( /^\d*\.\d*[1-9]$/ ) != null );
}

//是否为正确的日期表达式(yyyy-mm-dd)
String.prototype.isDate = function()
{
	var r = this.match(/^(\d{4})(\-)(\d{1,2})\-(\d{1,2})$/);
	if( r == null )
	{
		return false;
	}
	else
	{
		var d = new Date(r[1],r[3]-1,r[4]);
		return( d.getFullYear() == r[1] && ( d.getMonth() + 1 ) == r[3] && d.getDate() == r[4] );
	}
}

//是否包含HTML标记
String.prototype.isContainHtml = function()
{
	//return ( this.match( /<\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>/ ) != null )
	//return ( this.match( /<\s*\/?\s*\S*\s*>/ ) != null )
	return ( this.match( /<+|>+/ ) != null );
}

//是否超过指定长度
String.prototype.isOverLength = function(maxLength)
{
	//将每一个双字节字符转换为"00",占两位长度
	var tempStr = this.replace(/[^\u0000-\u00ff]/g,"00");
	return ( tempStr.length > maxLength );
}

//是否少于指定长度
String.prototype.isUnderLength = function(minLength)
{
	//将每一个双字节字符转换为"00",占两位长度
	var tempStr = this.replace(/[^\u0000-\u00ff]/g,"00");
	return ( tempStr.length < minLength );
}

//是否是网址URL
String.prototype.isURL = function()
{
	if (this.substring(0,11)=='javascript:') return true;
	return ( this.match( /^(http:\/\/)?([\w-]+\.)+[\w-]+(\?[\w-]+=[^?&=]+)?(&[\w-]+=[^?&=]+)*$/ ) != null );
}

//是否是正确的电话号码
String.prototype.isTelephone = function()
{
	return ( this.match( /^((\+?\d+([-－]\d+)+)|([\(\（]\+?\d+[\)\）]\d+)|(\d+))$/ ) != null );
}

//返回提示信息内容
function AlertInfo()
{
	this.isEmail = function(name)
	{
		if( name == null )
			return "不是正确的Email表达式！"; 
		else
			return "\"" + name + "\"" + "不是正确的Email表达式！"; 
	}
	this.isEmpty = function(name)
	{
		if( name == null )
			return "不能为空！";
		else
			return "\"" + name + "\"" + "不能为空！";
	}
	
	this.isNumeric = function(name,b)
	{
		if( name == null )
			if( b ) return "不能为数字！";
			else return "必须是数字！";
		else
			if( b ) return "\"" + name + "\"" + "不能为数字！";
			else return "\"" + name + "\"" + "必须是数字！";
	}
	
	this.isInteger = function(name,b)
	{
		if( name == null )
			if( b ) return "不能为整数！";
			else return "必须是整数！";
		else
			if( b ) return "\"" + name + "\"" + "不能为整数！";
			else return "\"" + name + "\"" + "必须是整数！";
	}
	
	this.isPlusInteger = function(name,b)
	{
		if( name == null )
			if( b ) return "不能为0或正整数！";
			else return "必须是0或正整数！";
		else
			if( b ) return "\"" + name + "\"" + "不能为0或正整数！";
			else return "\"" + name + "\"" + "必须是0或正整数！";
	}
	
	this.isDecimal = function(name,b)
	{
		if( name == null )
			if( b ) return "不能为小数！";
			else return "必须是小数！";
		else
			if( b ) return "\"" + name + "\"" + "不能为小数！";
			else return "\"" + name + "\"" + "必须是小数！";
	}
	
	this.isPlusDecimal = function(name,b)
	{
		if( name == null )
			if( b ) return "不能为正小数！";
			else return "必须是正小数！";
		else
			if( b ) return "\"" + name + "\"" + "不能为正小数！";
			else return "\"" + name + "\"" + "必须是正小数！";
	}
	
	this.isDate = function(name)
	{
		if( name == null )
			return "不是正确的日期表达式！"; 
		else
			return "\"" + name + "\"" + "不是正确的日期表达式！"; 
	}
	
	this.isContainHtml = function(name)
	{
		if( name == null ) return "不能含有\"<\"、\">\"字符！";
		else return "\"" + name + "\"" + "不能含有\"<\"、\">\"字符！";
	}
	
	this.isOverLength = function(name,length)
	{
		var wordLen = parseInt(length/2);//字长，一个汉字占两个字符
		if( name == null )
			if( length == null )
				return "内容过长！";
			else
				return "不能超过" + wordLen + "个汉字(" + length + "个字符)!";
		else
			if( length == null )
				return "\"" + name + "\"" + "内容过长！";
			else
				return "\"" + name + "\"" + "不能超过" + wordLen + "个汉字(" + length + "个字符)!";
				
	}

	this.isUnderLength = function(name,length)
	{
		var wordLen = parseInt(length/2) + 1;//字长，一个汉字占两个字符
		if( name == null )
			if( length == null )
				return "内容过短！";
			else
				return "不能少于" + wordLen  + "个汉字(" + length + "个字符)!";
		else
			if( length == null )
				return "\"" + name + "\"" + "内容过短！";
			else
				return "\"" + name + "\"" + "不能少于" + wordLen + "个汉字(" + length + "个字符)!";
	}
	
	this.isURL = function(name,b)
	{
		if( name == null )
			if( b )
				return "不能是URL地址！";
			else
				return "错误的URL地址！";
		else
			if( b )
				return "\"" + name + "\"" + "不能是URL地址！";
			else
				return "\"" + name + "\"" + "错误的URL地址！";
	}
	
	this.isTelephone = function(name)
	{
		if( name == null )
			return "不正确的电话号码！";
		else
			return "\"" + name + "\"" + "格式不正确！";
	}
}