Java code
1.[size=12px]1。^\d+$ //匹配非负整数(正整数 + 0)
2。^[0-9]*[1-9][0-9]*$ //匹配正整数
3。^((-\d+)|(0+))$ //匹配非正整数(负整数 + 0)
4。^-[0-9]*[1-9][0-9]*$ //匹配负整数
5。^-?\d+$ //匹配整数
6。^\d+(\.\d+)?$ //匹配非负浮点数(正浮点数 + 0)
7。^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮点数
8。^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮点数(负浮点数 + 0)
9。^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配负浮点数
10。^(-?\d+)(\.\d+)?$ //匹配浮点数
11。^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
12。^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
13。^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
14。^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
15。^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
16。^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
17。^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url
18。匹配中文字符的正则表达式: [\u4e00-\u9fa5]
19。匹配双字节字符(包括汉字在内):[^\x00-\xff]
20。应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
21。匹配空行的正则表达式:\n[\s| ]*\r
22。匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/
23。匹配首尾空格的正则表达式:(^\s*)|(\s*$)
* 正则表达式用例
* 1、^\S+[a-z A-Z]$ 不能为空 不能有空格 只能是英文字母
* 2、\S{6,} 不能为空 六位以上
* 3、^\d+$ 不能有空格 不能非数字
* 4、(.*)(\.jpg|\.bmp)$ 只能是jpg和bmp格式
* 5、^\d{4}\-\d{1,2}-\d{1,2}$ 只能是2004-10-22格式
* 6、^0$ 至少选一项
* 7、^0{2,}$ 至少选两项
* 8、^[\s|\S]{20,}$ 不能为空 二十字以上
* 9、^\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\.|\-))+[a-z]{2,6}$邮件
* 10、\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)* 输入多个地址用逗号或空格分隔邮件
* 11、^(\([0-9]+\))?[0-9]{7,8}$电话号码7位或8位或前面有区号例如(022)87341628
* 12、^[a-z A-Z 0-9 _]+@[a-z A-Z 0-9 _]+(\.[a-z A-Z 0-9 _]+)+(\,[a-z A-Z 0-9 _]+@[a-z A-Z 0-9 _]+(\.[a-z A-Z 0-9 _]+)+)*$
* 只能是字母、数字、下划线;必须有@和.同时格式要规范 邮件
* 13 ^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$上面表达式也可以写成这样子,更精练。
14 ^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$ [/size]
/*------------------------
功能:替换任何空白字符
-------------------------*/
function TrimString (strVal)
{
strTmp = strVal + "";
if (strTmp.length == 0)
return (strTmp);
reVal = /^/s*/;
strTmp = strTmp.replace (reVal, '');
reVal = //s*$/;
return (strTmp.replace (reVal, ''));
}
/*------------------------
功能:检测是否是有效数字
-------------------------*/
function Check_Num( num )
{
num = ( TrimString( num ) );
if (num.length == 0)
return (false);
return ( Number( num ) );
}
/*------------------------
功能:检测是否是有效日期
-------------------------*/
function Check_Date (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[//|/-](0?[1-9]|10|11|12)[//|/-]([1-2]?[0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*------------------------
功能:检测是否是有效Email
-------------------------*/
function Check_Email (strEmail)
{
strEmail = (TrimString (strEmail));
if (strEmail.length == 0)
return (false);
/^\w{3,}@\w+(\.\w+)+$/
reVal = /^[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+@[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+(/.[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+)+$/;
return (reVal.test (strEmail));
}
/*------------------------
功能:检测是否是有效时间
-------------------------*/
function Check_Time (strTime)
{
strTime = (TrimString (strTime));
if (strTime.length == 0)
return (false);
reVal = /^(([0-9]|[01][0-9]|2[0-3])(:([0-9]|[0-5][0-9])){0,2}|(0?[0-9]|1[0-1])(:([0-9]|[0-5][0-9])){0,2}/s?[aApP][mM])?$/;
return (reVal.test (strTime));
}
/*------------------------
功能:检测是否是有效日期特定格式
-------------------------*/
function Check_Date_1 (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[//](0?[1-9]|10|11|12)[//]([1-2]?[0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*------------------------
功能:检测是否是有效日期特定格式
-------------------------*/
function Check_Date_2 (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[/-](0[1-9]|10|11|12)[/-]([1-2][0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*--------------------------------------
功能:换行定行
---------------------------------------*/
function enter( form, temp )
{
if ( window.event.keyCode == 13 )
{
eval( form + temp + ".focus()" );
eval( form + temp + ".select()" );
}
else
return (false);
}
/*--------------------------------------
功能:检查字符串长度
---------------------------------------*/
function ByteString (strVal)
{
nLen = 0;
for (i = 0; i < strVal.length; i ++)
{
if (strVal.charCodeAt (i) > 255)
nLen += 2;
else
nLen ++;
};
return (nLen);
}
/*--------------------------------------
功能:按要求截取字符串长度
---------------------------------------*/
function SubString(strVal,nStrLen)
{
nLen = 0;
nTemp = 0;
for (i = 0; i < strVal.length; i ++)
{
if (strVal.charCodeAt (i) > 255)
nLen += 2;
else
nLen ++;
if(nLen <= nStrLen)
nTemp = i;
else
break;
};
return(strVal.substr(0,nTemp+1));
}
/*------------------------
功能:检测密码,密码只能由英文字母、数字、减号、下划线、$、#、*、(和)构成,且首位必须是英文字母
-------------------------*/
function Check_Pass( strPass )
{
strPass = ( TrimString( strPass ) );
if (strPass.length == 0)
return (false);
reVal = /^[a-zA-Z]{1}[a-zA-Z0-9-_$#*()]{0,29}$/;
return ( reVal.test (strPass) );
}
1.[size=12px]1。^\d+$ //匹配非负整数(正整数 + 0)
2。^[0-9]*[1-9][0-9]*$ //匹配正整数
3。^((-\d+)|(0+))$ //匹配非正整数(负整数 + 0)
4。^-[0-9]*[1-9][0-9]*$ //匹配负整数
5。^-?\d+$ //匹配整数
6。^\d+(\.\d+)?$ //匹配非负浮点数(正浮点数 + 0)
7。^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮点数
8。^((-\d+(\.\d+)?)|(0+(\.0+)?))$ //匹配非正浮点数(负浮点数 + 0)
9。^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配负浮点数
10。^(-?\d+)(\.\d+)?$ //匹配浮点数
11。^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
12。^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
13。^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
14。^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
15。^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
16。^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ //匹配email地址
17。^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ //匹配url
18。匹配中文字符的正则表达式: [\u4e00-\u9fa5]
19。匹配双字节字符(包括汉字在内):[^\x00-\xff]
20。应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
21。匹配空行的正则表达式:\n[\s| ]*\r
22。匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/
23。匹配首尾空格的正则表达式:(^\s*)|(\s*$)
* 正则表达式用例
* 1、^\S+[a-z A-Z]$ 不能为空 不能有空格 只能是英文字母
* 2、\S{6,} 不能为空 六位以上
* 3、^\d+$ 不能有空格 不能非数字
* 4、(.*)(\.jpg|\.bmp)$ 只能是jpg和bmp格式
* 5、^\d{4}\-\d{1,2}-\d{1,2}$ 只能是2004-10-22格式
* 6、^0$ 至少选一项
* 7、^0{2,}$ 至少选两项
* 8、^[\s|\S]{20,}$ 不能为空 二十字以上
* 9、^\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\.|\-))+[a-z]{2,6}$邮件
* 10、\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)* 输入多个地址用逗号或空格分隔邮件
* 11、^(\([0-9]+\))?[0-9]{7,8}$电话号码7位或8位或前面有区号例如(022)87341628
* 12、^[a-z A-Z 0-9 _]+@[a-z A-Z 0-9 _]+(\.[a-z A-Z 0-9 _]+)+(\,[a-z A-Z 0-9 _]+@[a-z A-Z 0-9 _]+(\.[a-z A-Z 0-9 _]+)+)*$
* 只能是字母、数字、下划线;必须有@和.同时格式要规范 邮件
* 13 ^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$上面表达式也可以写成这样子,更精练。
14 ^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$ [/size]
/*------------------------
功能:替换任何空白字符
-------------------------*/
function TrimString (strVal)
{
strTmp = strVal + "";
if (strTmp.length == 0)
return (strTmp);
reVal = /^/s*/;
strTmp = strTmp.replace (reVal, '');
reVal = //s*$/;
return (strTmp.replace (reVal, ''));
}
/*------------------------
功能:检测是否是有效数字
-------------------------*/
function Check_Num( num )
{
num = ( TrimString( num ) );
if (num.length == 0)
return (false);
return ( Number( num ) );
}
/*------------------------
功能:检测是否是有效日期
-------------------------*/
function Check_Date (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[//|/-](0?[1-9]|10|11|12)[//|/-]([1-2]?[0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*------------------------
功能:检测是否是有效Email
-------------------------*/
function Check_Email (strEmail)
{
strEmail = (TrimString (strEmail));
if (strEmail.length == 0)
return (false);
/^\w{3,}@\w+(\.\w+)+$/
reVal = /^[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+@[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+(/.[/-!#/$%&'/*/+///.//0-9=/?A-Z/^_`a-z{|}~]+)+$/;
return (reVal.test (strEmail));
}
/*------------------------
功能:检测是否是有效时间
-------------------------*/
function Check_Time (strTime)
{
strTime = (TrimString (strTime));
if (strTime.length == 0)
return (false);
reVal = /^(([0-9]|[01][0-9]|2[0-3])(:([0-9]|[0-5][0-9])){0,2}|(0?[0-9]|1[0-1])(:([0-9]|[0-5][0-9])){0,2}/s?[aApP][mM])?$/;
return (reVal.test (strTime));
}
/*------------------------
功能:检测是否是有效日期特定格式
-------------------------*/
function Check_Date_1 (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[//](0?[1-9]|10|11|12)[//]([1-2]?[0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*------------------------
功能:检测是否是有效日期特定格式
-------------------------*/
function Check_Date_2 (strDate)
{
strDate = (TrimString (strDate));
if (strDate.length == 0)
return (false);
reVal = /^([1-2]/d{3})[/-](0[1-9]|10|11|12)[/-]([1-2][0-9]|0[1-9]|30|31)$/;
return (reVal.test (strDate));
}
/*--------------------------------------
功能:换行定行
---------------------------------------*/
function enter( form, temp )
{
if ( window.event.keyCode == 13 )
{
eval( form + temp + ".focus()" );
eval( form + temp + ".select()" );
}
else
return (false);
}
/*--------------------------------------
功能:检查字符串长度
---------------------------------------*/
function ByteString (strVal)
{
nLen = 0;
for (i = 0; i < strVal.length; i ++)
{
if (strVal.charCodeAt (i) > 255)
nLen += 2;
else
nLen ++;
};
return (nLen);
}
/*--------------------------------------
功能:按要求截取字符串长度
---------------------------------------*/
function SubString(strVal,nStrLen)
{
nLen = 0;
nTemp = 0;
for (i = 0; i < strVal.length; i ++)
{
if (strVal.charCodeAt (i) > 255)
nLen += 2;
else
nLen ++;
if(nLen <= nStrLen)
nTemp = i;
else
break;
};
return(strVal.substr(0,nTemp+1));
}
/*------------------------
功能:检测密码,密码只能由英文字母、数字、减号、下划线、$、#、*、(和)构成,且首位必须是英文字母
-------------------------*/
function Check_Pass( strPass )
{
strPass = ( TrimString( strPass ) );
if (strPass.length == 0)
return (false);
reVal = /^[a-zA-Z]{1}[a-zA-Z0-9-_$#*()]{0,29}$/;
return ( reVal.test (strPass) );
}