一些常用的正则表达式

用正则表达式啊。<script type="text/javascript">
function isUsername(s)
{
var patrn=/([A-Za-z]{0,1}[0-9]{1,2}|[0-9]{0,1}[A-Za-z]{1,2})[A-Za-z0-9]+$/;
if (!patrn.exec(s))
{
alert("用户名出错![注释:只能由数字和字母组成]");
return false;
}
return true;
}
</script>
<html>
用户名:<input type="text" value="" onblur="isUsername(this.value)" />
</html>
function f_MobilCheck(as_SourceString)
{
 if(as_SourceString.match(/^13[0-9]{9}$/g)) return true;  //手机号为13开头的11位数字
 else if(as_SourceString.match(/^[0]{1}[0-9]{2,3}[2-8]{1}[0-9]{5,7}$/g)) return true;  //小灵通为0开头的3-4位的区号+不以1和9开头的6-8位数字
 return false;
}
 //---------------------------------------------验证是否合法的电子邮箱地址----合法:true---不合法:false---------
function f_EmailCheck(as_SourceString)
{
 return as_SourceString.match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/g);
}
//----------------------------------------------验证是否不含有非法的字符--不含有,即合法:true---不合法:false-------------
function f_StringCheck(as_SourceString)
{
 //非法字符--(双减号)/*(斜杠星号)'(单引号);(分号)"(双引号)%(百分号)<(左尖括号)>(右尖括号)
 if(as_SourceString.match(/\/\*|-{2}|[';"%<>]+/)) return false;
 else return true;
}
//-------------------------------验证字符串长度是否符合要求---0-为空,1-为小于下限,2-为大于上限,3-符合要求---
function f_StringLenCheck(as_SourceString, low_Length, up_Length)
{
 //字符串长度,单字节计1,双字节计和汉字等计2
 as_SourceString = as_SourceString.replace(/[^\x00-\xff]/g,"aa");
 if(as_SourceString.length == 0) return 0;
 else if(as_SourceString.length < low_Length) return 1;
 else if(as_SourceString.length > up_Length) return 2;
 else return 3;
}
//---------------------------------------------验证是否全部是数字且不以0开头----合法:true---不合法:false---------
function f_NumericCheck(as_SourceString)
{
 return as_SourceString.match(/^[1-9]{1}\d*$/g);
}
//---------------------------------------------验证是否全部是数字可以0开头----合法:true---不合法:false---------
function f_NumericCheckAll(as_SourceString)
{
 return as_SourceString.match(/^[0-9]{1}\d*$/g);
}
//---------------------------------------------验证是否为标准的电话号码----合法:true---不合法:false---------
function f_MobilCheck(as_SourceString)
{
 if(as_SourceString.match(/^13[0-9]{9}$/g)) return true;  //手机号为13开头的11位数字
 else if(as_SourceString.match(/^[0]{1}[0-9]{2,3}[2-8]{1}[0-9]{5,7}$/g)) return true;  //小灵通为0开头的3-4位的区号+不以1和9开头的6-8位数字
 return false;
}
//---------------------------------------------验证是否为标准的身份证号码----合法:true---不合法:false---------
function f_IDCardCheck(as_SourceString)
{
 return as_SourceString.match(/^[0-9]{6}[1-2]{1}[0-9]{3}[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{1}[0-9]{3}[xX0-9]{1}$/g);
}
//----------------------------------------------验证短日期格式----------------------------------------------------
function f_DateShortCheck(as_SourceString)//2000-1-1或2000-01-01
{
 return as_SourceString.match(/^([1-2]{1})([0-9]{3})-(0?[1-9]|10|11|12)-(0?[1-9]|[1-2][0-9]|30|31)$/g);
}
{
 if(as_SourceString.match(/^13[0-9]{9}$/g)) return true;  //手机号为13开头的11位数字
 else if(as_SourceString.match(/^[0]{1}[0-9]{2,3}[2-8]{1}[0-9]{5,7}$/g)) return true;  //小灵通为0开头的3-4位的区号+不以1和9开头的6-8位数字
 return false;
}
 

posted on 2016-03-10 16:20  AnneQi  阅读(219)  评论(0)    收藏  举报