免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1843 | 回复: 1
打印 上一主题 下一主题

常用js正则表达式收藏 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-28 21:10 |只看该作者 |倒序浏览

常用js正则表达式收藏






/*  

用途:检查输入的Email信箱格式是否正确  

输入:strEmail:字符串  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function checkEmail(strEmail)   

  3. {

  4.     //var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;   

  5.     var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;  

  6.     if ( emailReg.test(strEmail) ) {  

  7.         return true;  

  8.     }  

  9.     else {  

  10.         alert("您输入的Email地址格式不正确!");  

  11.         return false;  

  12.     }  

  13. };

  14.   

  15. /*

  16. 用途:校验ip地址的格式  

  17. 输入:strIP:ip地址  

  18. 返回:如果通过验证返回true,否则返回false;  

  19. */

  20. function isIP(strIP)   

  21. {

  22.     if (isNull(strIP)) {  

  23.         return false;  

  24.     }  

  25.     var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式   

  26.     if (re.test(strIP)) {  

  27.         if ( RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) {

  28.             return true;  

  29.         }  

  30.     }  

  31.     return false;  

  32. };

  33.   

  34. /*  

  35. 用途:检查输入手机号码是否正确  

  36. 输入:strMobile:字符串  

  37. 返回:如果通过验证返回true,否则返回false  

  38. */

  39. function checkMobile( strMobile )  

  40. {

  41.     var regu = /^[1][3][0-9]{9}$/;  

  42.     var re = new RegExp(regu);  

  43.     if (re.test(strMobile)) {  

  44.         return true;  

  45.     }  

  46.     else {  

  47.         return false;  

  48.     }  

  49. };

  50.   

  51. /*  

  52. 用途:检查输入的电话号码格式是否正确  

  53. 输入:strPhone:字符串  

  54. 返回:如果通过验证返回true,否则返回false  

  55. */

  56. function checkPhone( strPhone )   

  57. {

  58.     var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/;  

  59.     var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;  

  60.     var prompt = "您输入的电话号码不正确!" if ( strPhone.length > 9 ) {  

  61.         if ( phoneRegWithArea.test(strPhone) ) {  

  62.             return true;  

  63.         }  

  64.         else {  

  65.             alert( prompt );  

  66.             return false;  

  67.         }  

  68.     }  

  69.     else {  

  70.         if ( phoneRegNoArea.test( strPhone ) ) {  

  71.             return true;  

  72.         }  

  73.         else {  

  74.             alert( prompt );  

  75.             return false;  

  76.         }  

  77.     }  

  78. };

  79.   

  80. /*  

  81. 用途:检查输入字符串是否为空或者全部都是空格  

  82. 输入:str  

  83. 返回:如果全是空返回true,否则返回false  

  84. */

  85. function isNull( str )  

  86. {

  87.     if ( str == "" ) {  

  88.         return true;  

  89.     }  

  90.     var regu = "^[ ]+$";  

  91.     var re = new RegExp(regu);  

  92.     return re.test(str);  

  93. };

  94.   

  95. /*  

  96. 用途:检查输入对象的值是否符合整数格式  

  97. 输入:str 输入的字符串  

  98. 返回:如果通过验证返回true,否则返回false  

  99. */

  100. function isInteger( str )  

  101. {

  102.     var regu = /^[-]{0,1}[0-9]{1,}$/;  

  103.     return regu.test(str);  

  104. };

  105.   

  106. /*  

  107. 用途:检查输入字符串是否符合正整数格式  

  108. 输入:s:字符串  

  109. 返回:如果通过验证返回true,否则返回false  

  110. */

  111. function isNumber( s )  

  112. {

  113.     var regu = "^[0-9]+$";  

  114.     var re = new RegExp(regu);  

  115.     if (s.search(re) != - 1) {  

  116.         return true;  

  117.     }  

  118.     else {  

  119.         return false;  

  120.     }  

  121. };

  122.   

  123. /*  

  124. 用途:检查输入字符串是否是带小数的数字格式,可以是负数  

  125. 输入:str:字符串  

  126. 返回:如果通过验证返回true,否则返回false  

  127. */

  128. function isDecimal( str )  

  129. {

  130.     if (isInteger(str)) {  

  131.         return true;  

  132.     }  

  133.     var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/;  

  134.     if (re.test(str)) {  

  135.         if (RegExp.$1 == 0 && RegExp.$2 == 0) {  

  136.             return false;  

  137.         }  

  138.         return true;  

  139.     }  

  140.     else {  

  141.         return false;  

  142.     }  

  143. };

  144.   

  145. /*  

  146. 用途:检查输入对象的值是否符合端口号格式  

  147. 输入:str 输入的字符串  

  148. 返回:如果通过验证返回true,否则返回false  

  149. */

  150. function isPort( str )  

  151. {

  152.     return (isNumber(str) && str < 65536);  

  153. };

  154.   

  155. /*  

  156. 用途:检查输入字符串是否符合金额格式,格式定义为带小数的正数,小数点后最多三位  

  157. 输入:s:字符串  

  158. 返回:如果通过验证返回true,否则返回false  

  159. */

  160. function isMoney( s )  

  161. {

  162.     var regu = "^[0-9]+[\.][0-9]{0,3}$";  

  163.     var re = new RegExp(regu);  

  164.     if (re.test(s)) {  

  165.         return true;  

  166.     }  

  167.     else {  

  168.         return false;  

  169.     }  

  170. };

  171.   

  172. /*  

  173. 用途:检查输入字符串是否只由英文字母和数字和下划线组成  

  174. 输入:s:字符串  

  175. 返回:如果通过验证返回true,否则返回false  

  176. */

  177. function isNumberOr_Letter( s )  

  178. {

  179.     //判断是否是数字或字母   

  180.     var regu = "^[0-9a-zA-Z\_]+$";  

  181.     var re = new RegExp(regu);  

  182.     if (re.test(s)) {  

  183.         return true;  

  184.     }  

  185.     else {  

  186.         return false;  

  187.     }  

  188. };

  189.   

  190. /*  
复制代码
用途:检查输入字符串是否只由英文字母和数字组成  

输入:s:字符串  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function isNumberOrLetter( s )  

  3. {
复制代码
//判断是否是数字或字母
  1. var regu = "^[0-9a-zA-Z]+$";  

  2.     var re = new RegExp(regu);  

  3.     if (re.test(s)) {  

  4.         return true;  

  5.     }  

  6.     else {  

  7.         return false;  

  8.     }  

  9. };

  10.   

  11. /*  
复制代码
用途:检查输入字符串是否只由汉字、字母、数字组成  

输入:s:字符串  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function isChinaOrNumbOrLett( s )  

  3. {

  4.     //判断是否是汉字、字母、数字组成   

  5.     var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";  

  6.     var re = new RegExp(regu);  

  7.     if (re.test(s)) {  

  8.         return true;  

  9.     }  

  10.     else {  

  11.         return false;  

  12.     }  

  13. };

  14.   

  15. /*  
复制代码
用途:判断是否是日期  

输入:date:日期;fmt:日期格式  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function isDate( date, fmt )   

  3. {

  4.     if (fmt == null) {  

  5.         fmt = "yyyyMMdd";  

  6.     }  

  7.     var yIndex = fmt.indexOf("yyyy");  

  8.     if (yIndex ==- 1) {  

  9.         return false;  

  10.     }  

  11.     var year = date.substring(yIndex, yIndex + 4);  

  12.     var mIndex = fmt.indexOf("MM");  

  13.     if (mIndex ==- 1) {  

  14.         return false;  

  15.     }  

  16.     var month = date.substring(mIndex, mIndex + 2);  

  17.     var dIndex = fmt.indexOf("dd");  

  18.     if (dIndex ==- 1) {  

  19.         return false;  

  20.     }  

  21.     var day = date.substring(dIndex, dIndex + 2);  

  22.     if (!isNumber(year) || year > "2100" || year < "1900") {  

  23.         return false;  

  24.     }  

  25.     if (!isNumber(month) || month > "12" || month < "01") {  

  26.         return false;  

  27.     }  

  28.     if (day > getMaxDay(year, month) || day < "01") {  

  29.         return false;  

  30.     }  

  31.     return true;  

  32. };

  33. function getMaxDay(year, month)   

  34. {

  35.     if (month == 4 || month == 6 || month == 9 || month == 11) {  

  36.         return "30";  

  37.     }  

  38.     if (month == 2) {  

  39.         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {  

  40.             return "29";  

  41.         }  

  42.         else {  

  43.             return "28";  

  44.         }  

  45.         return "31";;  

  46.     }  

  47. };

  48.   

  49. /*  
复制代码
用途:字符1是否以字符串2结束  

输入:str1:字符串;str2:被包含的字符串  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function isLastMatch(str1, str2)   

  3. {

  4.     var index = str1.lastIndexOf(str2);  

  5.     if (str1.length == index + str2.length) {  

  6.         return true;  

  7.     }  

  8.     return false;  

  9. };

  10.   

  11. /*  
复制代码
用途:字符1是否以字符串2开始  

输入:str1:字符串;str2:被包含的字符串  

返回:如果通过验证返回true,否则返回false

  1. */

  2. function isFirstMatch(str1, str2)   

  3. {

  4.     var index = str1.indexOf(str2);  

  5.     if (index == 0) {  

  6.         return true;  

  7.     }  

  8.     return false;  

  9. };

  10.   

  11. /*  
复制代码
用途:字符1是包含字符串2  

输入:str1:字符串;str2:被包含的字符串  

返回:如果通过验证返回true,否则返回false

  1. */

  2. function isMatch(str1, str2)   

  3. {

  4.     var index = str1.indexOf(str2);  

  5.     if (index ==- 1) {  

  6.         return false;  

  7.     }  

  8.     return true;  

  9. };

  10.   

  11. /*  
复制代码
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确,且结束如期>=起始日期  

输入:startDate:起始日期,字符串; endDate:结束如期,字符串  

返回:如果通过验证返回true,否则返回false

  1. */

  2. function checkTwoDate( startDate, endDate )   

  3. {

  4.     if ( !isDate(startDate) ) {  

  5.         alert("起始日期不正确!");  

  6.         return false;  

  7.     }  

  8.     else if ( !isDate(endDate) ) {  

  9.         alert("终止日期不正确!");  

  10.         return false;  

  11.     }  

  12.     else if ( startDate > endDate ) {  

  13.         alert("起始日期不能大于终止日期!");  

  14.         return false;  

  15.     }  

  16.     return true;  

  17. };

  18.   

  19. /*  
复制代码
用途:检查复选框被选中的数目  

输入:checkboxID:字符串  

返回:返回该复选框中被选中的数目
  1. */

  2. function checkSelect( checkboxID )   

  3. {

  4.     var check = 0;  

  5.     var i = 0;  

  6.     if ( document.all(checkboxID).length > 0 )   

  7.     {  

  8.         for ( i = 0; i < document.all(checkboxID).length; i++ ) {  

  9.             if ( document.all(checkboxID).item( i ).checked ) {  

  10.                 check += 1;  

  11.             }  

  12.         }  

  13.     }  

  14.     else {  

  15.         if ( document.all(checkboxID).checked ) {  

  16.             check = 1;  

  17.         }  

  18.     }  

  19.     return check;  

  20. }

  21. function getTotalBytes(varField)   

  22. {

  23.     if (varField == null) {  

  24.         return - 1;  

  25.     }  

  26.     var totalCount = 0;  

  27.     for (i = 0; i < varField.value.length; i++) {  

  28.         if (varField.value.charCodeAt(i) > 127) {  

  29.             totalCount += 2;  

  30.         }  

  31.         else {  

  32.             totalCount++ ;  

  33.         }  

  34.     }  

  35.     return totalCount;  

  36. }

  37. function getFirstSelectedValue( checkboxID )  

  38. {

  39.     var value = null;  

  40.     var i = 0;  

  41.     if ( document.all(checkboxID).length > 0 )  

  42.     {  

  43.         for ( i = 0; i < document.all(checkboxID).length; i++ )  

  44.         {  

  45.             if ( document.all(checkboxID).item( i ).checked ) {  

  46.                 value = document.all(checkboxID).item(i).value;  

  47.                 break;  

  48.             }  

  49.         }  

  50.     }  

  51.     else {  

  52.         if ( document.all(checkboxID).checked ) {  

  53.             value = document.all(checkboxID).value;  

  54.         }  

  55.     }  

  56.     return value;  

  57. }

  58. function getFirstSelectedIndex( checkboxID )  

  59. {

  60.     var value = - 2;  

  61.     var i = 0;  

  62.     if ( document.all(checkboxID).length > 0 )  

  63.     {  

  64.         for ( i = 0; i < document.all(checkboxID).length; i++ ) {  

  65.             if ( document.all(checkboxID).item( i ).checked ) {  

  66.                 value = i;  

  67.                 break;  

  68.             }  

  69.         }  

  70.     }  

  71.     else {  

  72.         if ( document.all(checkboxID).checked ) {  

  73.             value = - 1;  

  74.         }  

  75.     }  

  76.     return value;  

  77. }

  78. function selectAll( checkboxID, status )  

  79. {

  80.     if ( document.all(checkboxID) == null) {  

  81.         return;  

  82.     }  

  83.     if ( document.all(checkboxID).length > 0 )  

  84.     {  

  85.         for ( i = 0; i < document.all(checkboxID).length; i++ ) {  

  86.             document.all(checkboxID).item( i ).checked = status;  

  87.         }  

  88.     }  

  89.     else {  

  90.         document.all(checkboxID).checked = status;  

  91.     }  

  92. }

  93. function selectInverse( checkboxID )   

  94. {

  95.     if ( document.all(checkboxID) == null) {  

  96.         return;  

  97.     }  

  98.     if ( document.all(checkboxID).length > 0 )   

  99.     {  

  100.         for ( i = 0; i < document.all(checkboxID).length; i++ )   

  101.         {  

  102.             document.all(checkboxID).item( i ).checked = !document.all(checkboxID).item( i ).checked;

  103.         }  

  104.     }  

  105.     else {  

  106.         document.all(checkboxID).checked = !document.all(checkboxID).checked;  

  107.     }  

  108. }

  109. function checkDate( value )   

  110. {

  111.     if (value == '') {  

  112.         return true;  

  113.     }  

  114.     if (value.length != 8 || !isNumber(value)) {  

  115.         return false;  

  116.     }  

  117.     var year = value.substring(0, 4);  

  118.     if (year > "2100" || year < "1900") {  

  119.         return false;  

  120.     }  

  121.     var month = value.substring(4, 6);  

  122.     if (month > "12" || month < "01") {  

  123.         return false;  

  124.     }  

  125.     var day = value.substring(6, 8);  

  126.     if (day > getMaxDay(year, month) || day < "01") {  

  127.         return false;  

  128.     }  

  129.     return true;  

  130. };

  131.   

  132. /*  
复制代码
用途:检查输入的起止日期是否正确,规则为两个日期的格式正确或都为空且结束日期>=起始日期  

输入:startDate:起始日期,字符串; endDate: 结束日期,字符串  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function checkPeriod( startDate, endDate )   

  3. {

  4.     if ( !checkDate(startDate) ) {  

  5.         alert("起始日期不正确!");  

  6.         return false;  

  7.     }  

  8.     else if ( !checkDate(endDate) ) {  

  9.         alert("终止日期不正确!");  

  10.         return false;  

  11.     }  

  12.     else if ( startDate > endDate ) {  

  13.         alert("起始日期不能大于终止日期!");  

  14.         return false;  

  15.     }  

  16.     return true;  

  17. };

  18.   

  19. /*  
复制代码
用途:检查证券代码是否正确  

输入:secCode:证券代码  

返回:如果通过验证返回true,否则返回false
  1. */

  2. function checkSecCode( secCode )   

  3. {

  4.     if ( secCode.length != 6 ) {  

  5.         alert("证券代码长度应该为6位");  

  6.         return false;  

  7.     }  

  8.     if (!isNumber( secCode ) ) {  

  9.         alert("证券代码只能包含数字");  

  10.         return false;  

  11.     }  

  12.     return true;  

  13. };

  14.   

  15. /*

  16. function:cTrim(sInputString,iType)  
复制代码
description:字符串去空格的函数  

parameters:iType:1=去掉字符串左边的空格;2=去掉字符串左边的空格;0=去掉字符串左边和右边的空格  

return value:去掉空格的字符串
  1. */

  2. function cTrim(sInputString, iType)   

  3. {

  4.     var sTmpStr = ' ';  

  5.     var i = - 1;  

  6.     if (iType == 0 || iType == 1)   

  7.     {  

  8.         while (sTmpStr == ' ') {  

  9.             ++i;  

  10.             sTmpStr = sInputString.substr(i, 1);  

  11.         }  

  12.         sInputString = sInputString.substring(i);  

  13.     }  

  14.     if (iType == 0 || iType == 2)   

  15.     {  

  16.         sTmpStr = ' ';  

  17.         i = sInputString.length;  

  18.         while (sTmpStr == ' ') {  

  19.             --i;  

  20.             sTmpStr = sInputString.substr(i, 1);  

  21.         }  

  22.         sInputString = sInputString.substring(0, i + 1);  

  23.     }  

  24.     return sInputString;  

  25. };
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-28 21:10 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP