常用的vbscript正则检测函数

ASP/Visual Basic代码
  1. '----------------------------------------------------------   
  2. ' Function Name : Length   
  3. ' Function Desc : 返回字符串的实际长度, 一个汉字算2个长度   
  4. '---------------------------------------------------------   
  5. Public Function Length(sInput)   
  6.   
  7. Dim oregExp   
  8.   
  9. '建立正则表达式   
  10. Set oregExp = New RegExp   
  11.   
  12. '设置模式   
  13. oregExp.Pattern = "[^\x00-\xff]"  
  14. '设置是否区分字符大小写   
  15. oregExp.IgnoreCase = True  
  16. '设置全局可用性   
  17. oregExp.Global = True  
  18.   
  19. '执行搜索   
  20. Length = Len(oRegExp.Replace(sInput, "**"))   
  21.   
  22. Set oregExp = Nothing  
  23.   
  24. End Function  
  25.   
  26. '-----------------------------------------------------------------   
  27. ' Function Name : IsValidDate   
  28. ' Function Desc : 判断输入是否是有效的短日期格式 - "YYYY-MM-DD"   
  29. '----------------------------------------------------------------   
  30. Public Function IsValidDate(sInput)   
  31.   
  32. Dim oregExp   
  33.   
  34. '建立正则表达式   
  35. Set oregExp = New RegExp   
  36.   
  37. '设置模式   
  38. oregExp.Pattern = "^\d{4}-\d{2}-\d{2}$"  
  39. '设置是否区分字符大小写   
  40. oregExp.IgnoreCase = True  
  41. '设置全局可用性   
  42. oregExp.Global = True  
  43.   
  44. '执行搜索   
  45. If oregExp.Test(sInput) Then  
  46. IsValidDate = IsDate(sInput)   
  47. Else  
  48. IsValidDate = False  
  49. End If  
  50.   
  51. Set oregExp = Nothing  
  52.   
  53. End Function  
  54.   
  55. '-------------------------------------------------------------   
  56. ' Function Name : IsValidTime   
  57. ' Function Desc : 判断输入是否是有效的时间格式 - "HH:MM:SS"   
  58. '--------------------------------------------------------------   
  59. Public Function IsValidTime(sInput)   
  60.   
  61. Dim oregExp   
  62.   
  63. '建立正则表达式   
  64. Set oregExp = New RegExp   
  65.   
  66. '设置模式   
  67. oregExp.Pattern = "^\d{2}:\d{2}:\d{2}$"  
  68. '设置是否区分字符大小写   
  69. oregExp.IgnoreCase = True  
  70. '设置全局可用性   
  71. oregExp.Global = True  
  72.   
  73. '执行搜索   
  74. If oregExp.Test(sInput) Then  
  75. IsValidTime = IsDate(sInput)   
  76. Else  
  77. IsValidTime = False  
  78. End If  
  79.   
  80. Set oregExp = Nothing  
  81.   
  82. End Function  
  83.   
  84. '---------------------------------------------------------   
  85. ' Function Name : IsValidEmail   
  86. ' Function Desc : 判断输入是否是有效的电子邮件   
  87. '---------------------------------------------------------   
  88. Public Function IsValidEmail(sInput)   
  89.   
  90. Dim oregExp   
  91.   
  92. '建立正则表达式   
  93. Set oregExp = New RegExp   
  94.   
  95. '设置模式   
  96. oregExp.Pattern = "^\w+((-\w+)|(\.\w))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"  
  97. '设置是否区分字符大小写   
  98. oregExp.IgnoreCase = True  
  99. '设置全局可用性   
  100. oregExp.Global = True  
  101.   
  102. '执行搜索   
  103. IsValidEmail = oregExp.Test(sInput)   
  104.   
  105. Set oregExp = Nothing  
  106.   
  107. End Function  
  108.   
  109. '------------------------------------------------------------   
  110. ' Function Name : IsValidDatetime   
  111. ' Function Desc : 判断输入是否是有效的长日期格式 - "YYYY-MM-DD HH:MM:SS"   
  112. '------------------------------------------------------------   
  113. Public Function IsValidDatetime(sInput)   
  114.   
  115. Dim oregExp   
  116.   
  117. '建立正则表达式   
  118. Set oregExp = New RegExp   
  119.   
  120. '设置模式   
  121. oregExp.Pattern = "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$"  
  122. '设置是否区分字符大小写   
  123. oregExp.IgnoreCase = True  
  124. '设置全局可用性   
  125. oregExp.Global = True  
  126.   
  127. '执行搜索   
  128. If oregExp.Test(sInput) Then  
  129. IsValidDatetime = IsDate(sInput)   
  130. Else  
  131. IsValidDatetime = False  
  132. End If  
  133.   
  134. Set oregExp = Nothing  
  135.   
  136. End Function  
  137.   
  138. '----------------------------------------------------------------   
  139. ' Function Name : IsValidInteger   
  140. ' Function Desc : 判断输入是否是一个整数   
  141. '----------------------------------------------------------------   
  142. Public Function IsValidInteger(sInput)   
  143.   
  144. Dim oregExp   
  145.   
  146. '建立正则表达式   
  147. Set oregExp = New RegExp   
  148.   
  149. '设置模式   
  150. oregExp.Pattern = "^(-|\+)?\d+$"  
  151. '设置是否区分字符大小写   
  152. oregExp.IgnoreCase = True  
  153. '设置全局可用性   
  154. oregExp.Global = True  
  155.   
  156. '执行搜索   
  157. IsValidInteger = oregExp.Test(sInput)   
  158.   
  159. Set oregExp = Nothing  
  160.   
  161. End Function  
  162.   
  163. '-------------------------------------------------------------   
  164. ' Function Name : IsValidPositiveInteger   
  165. ' Function Desc : 判断输入是否是一个正整数   
  166. '-----------------------------------------------------------   
  167. Public Function IsValidPositiveInteger(sInput)   
  168.   
  169. Dim oregExp   
  170.   
  171. '建立正则表达式   
  172. Set oregExp = New RegExp   
  173.   
  174. '设置模式   
  175. oregExp.Pattern = "^(\+)?\d+$"  
  176. '设置是否区分字符大小写   
  177. oregExp.IgnoreCase = True  
  178. '设置全局可用性   
  179. oregExp.Global = True  
  180.   
  181. '执行搜索   
  182. IsValidPositiveInteger = oregExp.Test(sInput)   
  183.   
  184. Set oregExp = Nothing  
  185.   
  186. End Function  
  187.   
  188. '-------------------------------------------------------------   
  189. ' Function Name : IsValidNegativeInteger   
  190. ' Function Desc : 判断输入是否是一个负整数   
  191. '-------------------------------------------------------------   
  192. Public Function IsValidNegativeInteger(sInput)   
  193.   
  194. Dim oregExp   
  195.   
  196. '建立正则表达式   
  197. Set oregExp = New RegExp   
  198.   
  199. '设置模式   
  200. oregExp.Pattern = "^-\d+$"  
  201. '设置是否区分字符大小写   
  202. oregExp.IgnoreCase = True  
  203. '设置全局可用性   
  204. oregExp.Global = True  
  205.   
  206. '执行搜索   
  207. IsValidNegativeInteger = oregExp.Test(sInput)   
  208.   
  209. Set oregExp = Nothing  
  210.   
  211. End Function  
  212.   
  213. '-------------------------------------------------------   
  214. ' Function Name : IsValidNumber   
  215. ' Function Desc : 判断输入是否是一个数字   
  216. '------------------------------------------------------   
  217. Public Function IsValidNumber(sInput)   
  218.   
  219. IsValidNumber =  IsNumeric(sInput)   
  220.   
  221. End Function  
  222.   
  223. '----------------------------------------------------------   
  224. ' Function Name : IsValidLetters   
  225. ' Function Desc : 判断输入是否是一个由 A-Z / a-z 组成的字符串   
  226. '----------------------------------------------------------   
  227. Public Function IsValidLetters(sInput)   
  228.   
  229. Dim oregExp   
  230.   
  231. '建立正则表达式   
  232. Set oregExp = New RegExp   
  233.   
  234. '设置模式   
  235. oregExp.Pattern = "^[a-zA-Z]+$"  
  236. '设置是否区分字符大小写   
  237. oregExp.IgnoreCase = True  
  238. '设置全局可用性   
  239. oregExp.Global = True  
  240.   
  241. '执行搜索   
  242. IsValidLetters = oregExp.Test(sInput)   
  243.   
  244. Set oregExp = Nothing  
  245.   
  246. End Function  
  247.   
  248. '----------------------------------------------------------   
  249. ' Function Name : IsValidDigits   
  250. ' Function Desc : 判断输入是否是一个由 0-9 组成的数字   
  251. '----------------------------------------------------------   
  252. Public Function IsValidDigits(sInput)   
  253.   
  254. Dim oregExp   
  255.   
  256. '建立正则表达式   
  257. Set oregExp = New RegExp   
  258.   
  259. '设置模式   
  260. oregExp.Pattern = "^[1-9][0-9]*$"  
  261. '设置是否区分字符大小写   
  262. oregExp.IgnoreCase = True  
  263. '设置全局可用性   
  264. oregExp.Global = True  
  265.   
  266. '执行搜索   
  267. IsValidDigits = oregExp.Test(sInput)   
  268.   
  269. Set oregExp = Nothing  
  270.   
  271. End Function  
  272.   
  273. '--------------------------------------------------------------   
  274. ' Function Name : IsValidAlphanumeric   
  275. ' Function Desc : 判断输入是否是一个由 0-9 / A-Z / a-z 组成的字符串   
  276. '---------------------------------------------------------------   
  277. Public Function IsValidAlphanumeric(sInput)   
  278.   
  279. Dim oregExp   
  280.   
  281. '建立正则表达式   
  282. Set oregExp = New RegExp   
  283.   
  284. '设置模式   
  285. oregExp.Pattern = "^[a-zA-Z0-9]+$"  
  286. '设置是否区分字符大小写   
  287. oregExp.IgnoreCase = True  
  288. '设置全局可用性   
  289. oregExp.Global = True  
  290.   
  291. '执行搜索   
  292. IsValidAlphanumeric = oregExp.Test(sInput)   
  293.   
  294. Set oregExp = Nothing  
  295.   
  296. End Function  
  297.   
  298. '-------------------------------------------------------------   
  299. ' Function Name : IsValidString   
  300. ' Function Desc : 判断输入是否是一个由 0-9 / A-Z / a-z / . / _ 组成的字符串   
  301. '-------------------------------------------------------------   
  302. Public Function IsValidString(sInput)   
  303.   
  304. Dim oregExp   
  305.   
  306. '建立正则表达式   
  307. Set oregExp = New RegExp   
  308.   
  309. '设置模式   
  310. oregExp.Pattern = "^[a-zA-Z0-9\s.\-_]+$"  
  311. '设置是否区分字符大小写   
  312. oregExp.IgnoreCase = True  
  313. '设置全局可用性   
  314. oregExp.Global = True  
  315.   
  316. '执行搜索   
  317. IsValidString = oregExp.Test(sInput)   
  318.   
  319. Set oregExp = Nothing  
  320.   
  321. End Function  
  322.   
  323. '-----------------------------------------------------------------   
  324. ' Function Name : IsValidPostalcode   
  325. ' Function Desc : 判断输入是否是一个有效的邮政编码   
  326. '----------------------------------------------------------------   
  327. Public Function IsValidPostalcode(sInput)   
  328.   
  329. Dim oregExp   
  330.   
  331. '建立正则表达式   
  332. Set oregExp = New RegExp   
  333.   
  334. '设置模式   
  335. oregExp.Pattern = "^\d{6}$"  
  336. '设置是否区分字符大小写   
  337. oregExp.IgnoreCase = True  
  338. '设置全局可用性   
  339. oregExp.Global = True  
  340.   
  341. '执行搜索   
  342. IsValidPostalcode = oregExp.Test(sInput)   
  343.   
  344. Set oregExp = Nothing  
  345.   
  346. End Function  
  347.   
  348. '-------------------------------------------------------------------   
  349. ' Function Name : IsValidPhoneNo   
  350. ' Function Desc : 判断输入是否是一个有效的电话号码   
  351. '-------------------------------------------------------------------   
  352. Public Function IsValidPhoneNo(sInput)   
  353.   
  354. Dim oregExp   
  355.   
  356. '建立正则表达式   
  357. Set oregExp = New RegExp   
  358.   
  359. '设置模式   
  360. oregExp.Pattern = "(^0\d{2,3}\-[1-9]\d{2,7}$)|(^[1-9]\d{2,7}$)|(^\(0[1-9]{2,3}\)[1-9]\d{2,7}$)"  
  361. '设置是否区分字符大小写   
  362. oregExp.IgnoreCase = True  
  363. '设置全局可用性   
  364. oregExp.Global = True  
  365.   
  366. '执行搜索   
  367. IsValidPhoneNo = oregExp.Test(sInput)   
  368.   
  369. Set oregExp = Nothing  
  370.   
  371. End Function  
  372.   
  373. '--------------------------------------------------------------------   
  374. ' Function Name : IsValidMobileNo   
  375. ' Function Desc : 判断输入是否是一个有效的手机号码   
  376. '---------------------------------------------------------------------   
  377. Public Function IsValidMobileNo(sInput)   
  378.   
  379. Dim oregExp   
  380.   
  381. '建立正则表达式   
  382. Set oregExp = New RegExp   
  383.   
  384. '设置模式   
  385. oregExp.Pattern = "^0?13\d{9}$"  
  386. '设置是否区分字符大小写   
  387. oregExp.IgnoreCase = True  
  388. '设置全局可用性   
  389. oregExp.Global = True  
  390.   
  391. '执行搜索   
  392. IsValidMobileNo = oregExp.Test(sInput)   
  393.   
  394. Set oregExp = Nothing  
  395.   
  396. End Function  


[本日志由 admin 于 2012-04-13 01:17 PM 更新]
上一篇: Asp编程常用的正则表达式
下一篇: 在b/s开发中经常用到的javaScript技术
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1855
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑