微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则参考

Javascript代码
  1. </pre><pre name="code" class="javascript">// 正则做参考,拷贝使用,注意正则前后加转义符

  2. var regexEnum =

  3. {

  4. intege:"^-?[1-9]\d*$",//整数

  5. intege1:"^[1-9]\d*$",//正整数

  6. intege2:"^-[1-9]\d*$",//负整数

  7. num:"^([+-]?)\d*\.?\d+$",//数字

  8. num1:"^[1-9]\d*|0$",//正数(正整数 + 0)

  9. num2:"^-[1-9]\d*|0$",//负数(负整数 + 0)

  10. decmal:"^([+-]?)\d*\.\d+$",//浮点数

  11. decmal1:"^[1-9]\d*.\d*|0.\d*[1-9]\d*$",   //正浮点数

  12. decmal2:"^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$",  //负浮点数

  13. decmal3:"^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0)$",  //浮点数

  14. decmal4:"^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$",   //非负浮点数(正浮点数 + 0)

  15. decmal5:"^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0$",  //非正浮点数(负浮点数 + 0)


  16. email:"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$",//邮件

  17. color:"^[a-fA-F0-9]{6}$",//颜色

  18. url:"^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$",//url

  19. chinese:"^[\u4E00-\u9FA5\uF900-\uFA2D]+$",//仅中文

  20. ascii:"^[\x00-\xFF]+$",//仅ACSII字符

  21. zipcode:"^\d{6}$",//邮编

  22. mobile:"^(13|15|18|14)[0-9]{9}$",//手机

  23. ip4:"^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$",//ip地址

  24. notempty:"^\S+$",//非空

  25. picture:"(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$",//图片

  26. rar:"(.*)\.(rar|zip|7zip|tgz)$",//压缩文件

  27. date:"^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$",//日期

  28. qq:"^[1-9]*[1-9][0-9]*$",//QQ号码

  29. tel:"^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$",//电话号码的函数(包括验证国内区号,国际区号,分机号)

  30. username:"^\w+$",//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串

  31. letter:"^[A-Za-z]+$",//字母

  32. letter_u:"^[A-Z]+$",//大写字母

  33. letter_l:"^[a-z]+$",//小写字母

  34. idcard:"^[1-9]([0-9]{14}|[0-9]{17})$",//***

  35. english:"^[A-Za-z]+$"

  36. }


  37. var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}


  38. function isCardID(sId){

  39. var iSum=0 ;

  40. var info="" ;

  41. if(!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的***长度或格式错误";

  42. sId=sId.replace(/x$/i,"a");

  43. if(aCity[parseInt(sId.substr(0,2))]==null) return "你的***地区非法";

  44. sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));

  45. var d=new Date(sBirthday.replace(/-/g,"/")) ;

  46. if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "***上的出生日期非法";

  47. for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charat(17 - i),11) ;

  48. if(iSum%11!=1) return "你输入的***号非法";

  49. return true;//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")

  50. }


  51. //自定义验证

  52. $.extend($.fn.validateBox.defaults.rules,{

  53. // 混合验证五个参数,第一个是正则表达式,第二个是错误提示信息,第三个是调用的url,第四个是传递给服务器的参数名称,第五个是错误提示信息

  54. Composite_validation: {

  55. validator: function(value,param) {

  56. var m_reg = new RegExp(param[0]); //传递过来的正则字符串中的"\"必须是"\\"

  57. if (!m_reg.test(value)) {

  58. $.fn.validateBox.defaults.rules.Composite_validation.message = param[1];

  59. return false;

  60. }

  61. else {

  62. var postdata = {};

  63. postdata[param[3]] = value;

  64. var result = $.ajax({

  65. url: param[2],

  66. data: postdata,

  67. async: false,

  68. type: "post"

  69. }).responseText;

  70. if (result == "false") {

  71. $.fn.validateBox.defaults.rules.Composite_validation.message = param[4];

  72. return false;

  73. }

  74. else {

  75. return true;

  76. }

  77. }

  78. },

  79. message: ''

  80. },

  81. equalTo: {

  82. validator: function(value,param) {

  83. // 做某些检查

  84. return value == $('#'+param[0]).val();

  85. },

  86. message: '两次输入的密码不一致'

  87. },

  88. mobile: {

  89. validator: function (value,param) {

  90. return /^(13|15|18|14)[0-9]{9}$/.test(value);

  91. },

  92. message: '手机号码不正确'

  93. },

  94. tel: {

  95. validator: function (value,param) {

  96. $.fn.validateBox.defaults.rules.tel.message = param[0];

  97. return /^\d{3,4}-\d{7,9}$/.test(value);

  98. },

  99. message: ''

  100. },

  101. number: {

  102. validator: function (value,param) {

  103. return /^\d+$/.test(value);

  104. },

  105. message: '请输入数字'

  106. },

  107. idcard: {

  108. validator: function (value,param) {

  109. var result = isCardID(value);

  110. if(result == true) {

  111. return true;

  112. } else {

  113. $.fn.validateBox.defaults.rules.idcard.message = result;

  114. return false;

  115. }

  116. },

  117. message:''

  118. },

  119. chinese: {

  120. validator: function(value,param) {

  121. return /^[\u4E00-\u9FA5\uF900-\uFA2D]+$/.test(value);

  122. },

  123. message: '只能输入中文'

  124. },

  125. english : {

  126. validator : function(value,param) {

  127. return /^\w+$/.test(value);

  128. },

  129. message : '只能输入英文'

  130. },

  131. unChinese : {

  132. validator : function(value,param) {

  133. //return /^[\w-.\/?%&=]*$/.test(value);

  134. if(/^[\u4E00-\u9FA5\uF900-\uFA2D]+$/.test(value)) {

  135. return false;

  136. }

  137. return true;

  138. },

  139. message : '能输入除中文以外的字符'

  140. }

  141. });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐