当前位置: 代码迷 >> JavaScript >> js 应验非常好_转
  详细解决方案

js 应验非常好_转

热度:418   发布时间:2012-10-08 19:54:56.0
js 验证非常好_转

http://justshare.iteye.com/blog/226479

?

  1. //the?common?event,If?your?brower?is?firefox,you?should?use?this?function?instead?of?"window.event" ??
  2. ????function?getEvent()?{??
  3. ????????if (document.all)???
  4. ????????????return ?window.event;? //get?ie?event ??
  5. ????????func=getEvent.caller;??
  6. ????????while (func!= null )?{??
  7. ????????????var?arg0=func.arguments[0 ]; //alert(arg0); ??
  8. ????????????if (arg0)?{??
  9. ????????????????if ((arg0.constructor==Event?||?arg0.constructor?==MouseEvent)?||?(typeof(arg0)== "object" ?&&?arg0.preventDefault?&&?arg0.stopPropagation))?{??
  10. ????????????????????return ?arg0;??
  11. ????????????????}?????????????
  12. ????????????}??
  13. ????????????func=func.caller;??
  14. ????????}??
  15. ????????return ? null ;??
  16. ????}??
  17. ????//去除左侧空格??? ??
  18. ????function?LTrim(str)?????
  19. ????{?????
  20. ?????return ?str.replace(/^\s*/g, "" );?????
  21. ????}??????
  22. ????????
  23. ????//去右空格??? ??
  24. ????function?RTrim(str)?????
  25. ????{?????
  26. ????return ?str.replace(/\s*$/g, "" );?????
  27. ????}?????
  28. ???????
  29. ???//去掉字符串两端的空格??? ??
  30. ???function?trim(str)?????
  31. ???{?????
  32. ????return ?str.replace(/(^\s*)|(\s*$)/g,? "" );??????
  33. ???}?????
  34. ??????
  35. ??//去除字符串中间空格??? ??
  36. ???function?CTim(str)?????
  37. ???{?????
  38. ????return ?str.replace(/\s/g, '' );?????
  39. ???}?????
  40. ??????
  41. ???//是否为由数字组成的字符串??? ??
  42. ???function?is_digitals(str)?????
  43. ??{?????
  44. ????var?reg=/^[0 - 9 ]*$/; //匹配整数??? ??
  45. ???return ?reg.test(str);???????????
  46. ???}?????
  47. ???????
  48. ???//验证是否为整数,包括正负数;??? ??
  49. ???function?Is_Int(str)?????
  50. ???{?????
  51. ????var?reg=/^(-|\+)?\d+$/;?????
  52. ????return ?reg.test(str);?????
  53. ???}?????
  54. ???????
  55. ???//是大于0的整数???? ??
  56. ???function?Is_positive_num(str)?????
  57. ???{?????
  58. ????var?reg=/^\d+$/;?????
  59. ????return ?reg.test(str);?????
  60. ???}?????
  61. ???????
  62. ???//负整数的验证??? ??
  63. ???function?Is_minus(str)?????
  64. ???{?????
  65. ????var?reg=/^-\d+$/;?????
  66. ????return ?reg.test(str);?????
  67. ???}?????
  68. ???????
  69. ???//验证是否为浮点数(正数)??? ??
  70. ???function?IsPositiveFloat(str)?????
  71. ???{?????
  72. ????var?check_float?=new ?RegExp( "^[1-9][0-9]*\.[0-9]+$" ); //匹配浮点数??? ??
  73. ????return ?check_float.exec(str);?????
  74. ???}?????
  75. ???????
  76. ??//是否为固定电话,区号3到4位,号码7到8位,区号和号码用"-"分割开,转接号码为1到6位,用小括号括起来紧跟在号码后面??? ??
  77. ??function?IsTelphone(str)?????
  78. ???{?????
  79. ????var?reg=/^[0 - 9 ]{ 3 , 4 }\-\d{ 7 , 8 }(\(\d{ 1 , 6 }\))?$/;?????
  80. ?????????????
  81. ???????if ?(reg.test(str))?????
  82. ?????return ? true ;?????
  83. ???????else ????
  84. ???????????return ? false ;?????
  85. ???}?????
  86. ???????
  87. ???//手机号码验证,验证13系列和158,159几种号码,长度11位??? ??
  88. ???function?IsMobel(str)?????
  89. ???{?????
  90. ???var?reg0?=?/^13 \d{ 9 }$/;?????
  91. ???????var?reg1?=?/^158 \d{ 8 }$/;?????
  92. ???????var?reg2?=?/^159 \d{ 8 }$/;?????
  93. ?????????
  94. ???????return ?(reg0.test(str)||reg1.test(str)||reg2.test(str))?????
  95. ??}?????
  96. ???????
  97. ???//验证是否为中文??? ??
  98. ??function?IsChinese(str)?????
  99. ???{?????
  100. ????var?reg=/^[\u0391-\uFFE5]+$/;?????
  101. ????return ?reg.test(str);?????
  102. ??}?????
  103. ???????
  104. ???//验证是否为qq号码,长度为5-10位??? ??
  105. ???function?IsQq(str)?????
  106. ???{?????
  107. ????var?reg=/^[1 - 9 ]\d{ 4 , 9 }$/;?????
  108. ????return ?reg.test(str);?????
  109. ???}?????
  110. ???????
  111. ???//验证邮编??? ??
  112. ???function?IsPostId(str)?????
  113. ???{?????
  114. ????var?reg=/^\d{6 }$/;?????
  115. ????return ?reg.test(str);?????
  116. ??}?????
  117. ??????
  118. ??//验证是否未email??? ??
  119. ??function?IsEmail(str)?????
  120. ??{?????
  121. ???var?reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;?????
  122. ???return ?reg.test(str);?????
  123. ??}?????
  124. ??????
  125. ??//验证IP地址??? ??
  126. ??function?IsIp(str)?????
  127. ??{?????
  128. ???var?check=function(v)?????
  129. ???{?????
  130. ????try ????
  131. ????{?????
  132. ?????return ?(v<= 255 ?&&?v>= 0 )?????
  133. ????}catch (x){?????
  134. ??????return ? false ;?????
  135. ?????}?????
  136. ???}?????
  137. ???var?re=str.split("." )?????
  138. ???return ?(re.length== 4 )?(check(re[ 0 ])?&&?check(re[ 1 ])?&&?check(re[ 2 ])?&&?check(re[ 3 ])): false ????
  139. ??}?????
  140. ??????
  141. ??//身份证验证??? ??
  142. ??function?IsIdnum(str)?????
  143. ??{?????
  144. ???var?City={11 : "北京" , 12 : "天津" , 13 : "河北" , 14 : "山西" , 15 : "内蒙古" , 21 : "辽宁" , 22 : "吉林" , 23 : "黑龙江?" ,?????
  145. ???????31 : "上海" , 32 : "江苏" , 33 : "浙江" , 34 : "安徽" , 35 : "福建" , 36 : "江西" , 37 : "山东" , 41 : "河南" , 42 : "湖北?" ,?????
  146. ???????43 : "湖南" , 44 : "广东" , 45 : "广西" , 46 : "海南" , 50 : "重庆" , 51 : "四川" , 52 : "贵州" , 53 : "云南" , 54 : "西藏?" ,?????
  147. ???????61 : "陕西" , 62 : "甘肃" , 63 : "青海" , 64 : "宁夏" , 65 : "新疆" , 71 : "台湾" , 81 : "香港" , 82 : "澳门" , 91 : "国外?" }?????
  148. ???var?iSum=0 ????
  149. ???var?info="" ????
  150. ???if (!/^\d{ 17 }(\d|x)$/i.test(str))?????
  151. ????return ? false ;?????
  152. ???str=str.replace(/x$/i,"a" );?????
  153. ???if (City[parseInt(str.substr( 0 , 2 ))]== null )?????
  154. ???{?????
  155. ????alert(?"Error:非法地区" );?????
  156. ????return ? false ;?????
  157. ???}?????
  158. ???sBirthday=str.substr(6 , 4 )+ "-" +Number(str.substr( 10 , 2 ))+ "-" +Number(str.substr( 12 , 2 ));?????
  159. ???var?d=new ?Date(sBirthday.replace(/-/g, "/" ))?????
  160. ???if (sBirthday!=(d.getFullYear()+ "-" +?(d.getMonth()+ 1 )?+? "-" ?+?d.getDate()))?????
  161. ???{?????
  162. ????alert("Error:非法生日" );?????
  163. ????return ? false ;?????
  164. ???}?????
  165. ???for (var?i?=? 17 ;i>= 0 ;i?--)??????
  166. ????iSum?+=?(Math.pow(2 ,i)?%? 11 )?*?parseInt(str.charAt( 17 ?-?i), 11 )?????
  167. ???if (iSum% 11 != 1 )?????
  168. ???{?????
  169. ????alert("Error:非法证号" );?????
  170. ????return ? false ;?????
  171. ???}?????
  172. ???return ?City[parseInt(str.substr( 0 , 2 ))]+ "," +sBirthday+ "," +(str.substr( 16 , 1 )% 2 ? "男" : "女" )?????
  173. ??}?????
  174. ??????
  175. ??//判断是否短时间,形如?(13:04:06)??? ??
  176. ??function?IsTime(str)?????
  177. ??{?????
  178. ???var?a?=?str.match(/^(\d{1 , 2 })(:)?(\d{ 1 , 2 })\ 2 (\d{ 1 , 2 })$/);?????
  179. ???if ?(a?==? null )??????
  180. ???{?????
  181. ????alert('输入的参数不是时间格式' );? return ? false ;?????
  182. ???}?????
  183. ???if ?(a[ 1 ]> 24 ?||?a[ 3 ]> 60 ?||?a[ 4 ]> 60 )?????
  184. ???{?????
  185. ????alert("时间格式不对" );?????
  186. ????return ? false ????
  187. ???}?????
  188. ???return ? true ;?????
  189. ??}?????
  190. ??????
  191. ??//短日期,形如?(2003-12-05)??? ??
  192. ??function?IsDate(str)?????
  193. ??{?????
  194. ???var?r?=?str.match(/^(\d{1 , 4 })(-|\/)(\d{ 1 , 2 })\ 2 (\d{ 1 , 2 })$/);?????
  195. ???if (r== null )?????
  196. ????return ? false ;?????
  197. ???var?d=?new ?Date(r[ 1 ],?r[ 3 ]- 1 ,?r[ 4 ]);?????
  198. ???return ?(d.getFullYear()==r[ 1 ]&&(d.getMonth()+ 1 )==r[ 3 ]&&d.getDate()==r[ 4 ]);?????
  199. ??}?????
  200. ??????
  201. ??//?长时间,形如?(2003-12-05?13:04:06)??? ??
  202. ??function?IsDateTime(str)?????
  203. ??{?????
  204. ???var?reg?=?/^(\d{1 , 4 })(-|\/)(\d{ 1 , 2 })\ 2 (\d{ 1 , 2 })?(\d{ 1 , 2 }):(\d{ 1 , 2 }):(\d{ 1 , 2 })$/;?????
  205. ???var?r?=?str.match(reg);?????
  206. ???if (r== null )?????
  207. ????return ? false ;?????
  208. ???var?d=?new ?Date(r[ 1 ],?r[ 3 ]- 1 ,r[ 4 ],r[ 5 ],r[ 6 ],r[ 7 ]);?????
  209. ???return ?(d.getFullYear()==r[ 1 ]&&(d.getMonth()+ 1 )==r[ 3 ]&&d.getDate()==r[ 4 ]&&d.getHours()==r[ 5 ]&&d.getMinutes()==r[ 6 ]&&d.getSeconds()==r[ 7 ]);?????
  210. ??}?????
  211. ??????
  212. ??//?判断字符全部由a-Z或者是A-Z的字字母组成??? ??
  213. ??function?Is_Letters(str)?????
  214. ??{?????
  215. ???var?reg=/[^a-zA-Z]/g;?????
  216. ???return ?reg.test(str);?????
  217. ??}?????
  218. ??????
  219. ??//?判断字符由字母和数字组成。??? ??
  220. ??function?Is_letter_num(str)?????
  221. ??{?????
  222. ???var?reg=/[^0 -9a-zA-Z]/g;?????
  223. ???return ?reg.test(str);?????
  224. ??}?????
  225. ??????
  226. ??//判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母??? ??
  227. ??function?IsUserName(str)?????
  228. ??{?????
  229. ???var?reg=/^([a-zA-z_]{1 })([\w]*)$/g;?????
  230. ???return ?reg.test(str);?????
  231. ??}?????
  232. ??????
  233. ??//?判断浏览器的类型??? ??
  234. ??function?GetBrowseType()?????
  235. ??{?????
  236. ???alert(window.navigator.appName);?????
  237. ??}?????
  238. ??????
  239. ??//判断ie的版本??? ??
  240. ??function?Get_Eidition()?????
  241. ??{?????
  242. ???alert(window.navigator.appVersion);?????
  243. ??}?????
  244. ??????
  245. ??//判断客户端的分辨率??? ??
  246. ??function?GetResolution()?????
  247. ??{?????
  248. ???alert(window.screen.height);?????
  249. ???alert(window.screen.width);?????
  250. ??}?????
  251. ??????
  252. ??//?判断用户名是否为数字字母下滑线????? ??
  253. ??function?notchinese(str)?????
  254. ??{??????
  255. ???var?reg=/[^A-Za-z0-9_]/g??????
  256. ??????if ?(reg.test(str))?????
  257. ???{??????
  258. ????????return ?( false );??????
  259. ??????}??????
  260. ???else ????
  261. ???{??????
  262. ????return ( true );?????
  263. ??????}??????
  264. ??}?????????
  265. ?????????????
  266. ??//验证url??? ??
  267. ??function?IsUrl(str)?????
  268. ??{?????
  269. ???var?reg=/^(http\:\/\/)?([a-z0-9 ][a-z0- 9 \-]+\.)?[a-z0- 9 ][a-z0- 9 \-]+[a-z0- 9 ](\.[a-z]{ 2 , 4 })+(\/[a-z0- 9 \.\,\-\_\%\?\=\&]?)?$/i;?????
  270. ???return ?reg.test(str);?????
  271. ??}?????
  272. ??????
  273. ??//判断是否含有汉字??????????? ??
  274. ??function?ContentWord(str)?????
  275. ??{??????
  276. ???if ?(escape(str).indexOf( "%u" )!=- 1 )??????
  277. ????return ? true ;?????
  278. ???else ????
  279. ????return ? false ;?????
  280. ??}?????
  281. ??????
  282. ??//页面里回车到下一控件的焦点??? ??
  283. ??function?Enter2Tab(e)?????
  284. ??{?????
  285. ??????try ????
  286. ??????{?????
  287. ??????????var?ob?=?IsFireFox???e.target?:?event.srcElement;?????
  288. ??????????if (ob.tagName?==? "INPUT" ?&&(ob.type?==? "text" ?||ob.type?==? "password" ?||ob.type?==? "checkbox" ??????
  289. ???????????||ob.type?==?"radio" )?||ob.tagName?==? "SELECT" )?????
  290. ??????????{?????
  291. ??????????????var?key?=?IsFireFox???e.which?:?event.keyCode;?????
  292. ??????????????if ?(key?==? 13 )?????
  293. ??????????????{?????
  294. ??????????????????if ?(IsFireFox)?????
  295. ??????????????????{?????
  296. ??????????????????????event.which?=?9 ;?????
  297. ??????????????????}?????
  298. ??????????????????else ????
  299. ??????????????????{?????
  300. ??????????????????????event.keyCode?=?9 ;?????
  301. ??????????????????}?????
  302. ??????????????}?????
  303. ??????????}?????
  304. ??????}?????
  305. ??????catch (E){}?????
  306. ??}?????
  307. ??????
  308. ??/**?? ?
  309. ??*?初始化一个xmlhttp对象?? ?
  310. ??*/ ????
  311. ??function?InitAjax()?????
  312. ??{?????
  313. ???var?ajax=false ;??????
  314. ?? ?try ??????
  315. ???{??????
  316. ??  ??ajax?=?new ?ActiveXObject( "Msxml2.XMLHTTP" );??????
  317. ?? ?}?catch ?(e)??????
  318. ????{??????
  319. ??  ???try ??????
  320. ?????{??????
  321. ??   ???ajax?=?new ?ActiveXObject( "Microsoft.XMLHTTP" );??????
  322. ??  ???}?catch ?(E)??????
  323. ??????{??????
  324. ??   ????ajax?=?false ;??????
  325. ??  ????}??????
  326. ?? ??}?????
  327. ?? ?if ?(!ajax?&&?typeof?XMLHttpRequest!= 'undefined' )??????
  328. ???{??????
  329. ??  ??ajax?=?new ?XMLHttpRequest();??????
  330. ?? ?}??????
  331. ?? ?return ?ajax;?????
  332. ??}?????
  333. ??????
  334. ??function?callback(ajax)?????
  335. ??{?????
  336. ???//如果执行是状态正常,那么就把返回的内容赋值给上面指定的层??? ??
  337. ??  if ?(ajax.readyState?==? 4 ?&&?ajax.status?==? 200 )??????
  338. ???{????????
  339. ??  ?show.innerHTML?=?ajax.responseText;??????
  340. ??  }??????
  341. ???else ????
  342. ???{?????
  343. ????alert("there?was?a?problem?retrieving?the?xml?data:" +ajax.statusText);?????
  344. ???}?????
  345. ??}?????
  346. ??????
  347. ??function?getNews(newsID)?????
  348. ??{?????
  349. ?? //如果没有把参数newsID传进来??? ??
  350. ?? if ?(typeof(newsID)?==? 'undefined' )?????
  351. ?? {??????
  352. ??  return ? false ;?????
  353. ?? }?????
  354. ?? //需要进行Ajax的URL地址??? ??
  355. ?? var?url?=?"show.php?id=" +?newsID;?????
  356. ?? //获取新闻显示层的位置??? ??
  357. ?? var?show?=?document.getElementById("show_news" );??????
  358. ?? //实例化Ajax对象??? ??
  359. ?? var?ajax?=?InitAjax();?????
  360. ??????
  361. ?? //使用Get方式进行请求??? ??
  362. ?? ajax.open("GET" ,url, true );??????
  363. ?? //获取执行状态??? ??
  364. ?? ajax.onreadystatechange?=function()?{?????
  365. ???if ?(ajax.readyState?==? 4 ?&&?ajax.status?==? 200 )??????
  366. ???{????????
  367. ??  ?show.innerHTML?=?ajax.responseText;??????
  368. ??  }??????
  369. ??}?????
  370. ?? //发送空??? ??
  371. ?? ajax.send(null );??????
  372. ??}??????
  373. ??????
  374. ??//_______全选择__________??? ??
  375. ??function?SelectAll()?????
  376. ??{?????
  377. ??????var?empty;?????
  378. ??????var?f?=?document.forms[0 ];?????
  379. ??????for ?(var?i?=? 0 ;?i?<?f.length;?i++)?????
  380. ??????{?????
  381. ??????????empty?=?f[i];?????
  382. ??????????if ?(empty.type?==? "checkbox" ?&&?empty.disabled?==? false )?????
  383. ??????????????empty.checked?=?true ;?????
  384. ??????}?????
  385. ??}?????
  386. ??????
  387. ??//__________返选择_________??? ??
  388. ??function?SelectReverse()?????
  389. ??{?????
  390. ??????var?empty;?????
  391. ??????var?f?=?document.forms[0 ];?????
  392. ??????for ?(var?i?=? 0 ;?i?<?f.length;?i++)?????
  393. ??????{?????
  394. ??????????empty?=?f[i];?????
  395. ??????????if ?(empty.type?==? "checkbox" ?&&?empty.disabled?==? false )?????
  396. ????if (empty.checked?==? true )?????
  397. ????{?????
  398. ???????empty.checked?=?false ;?????
  399. ????}?????
  400. ????else ????
  401. ????{?????
  402. ???????empty.checked?=?true ;?????
  403. ????}?????
  404. ??????}?????
  405. ??}
  相关解决方案