validate.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /****************************************
  2. * *
  3. * Form窗体脚本验证 *
  4. * *
  5. * 版本: V3.3 *
  6. * 修订日期:2012-08-06 *
  7. * *
  8. ****************************************/
  9. /*
  10. *V2.0 (2008-02-14)马超修改内容
  11. * 1、修改Form中对象的最大长度判断
  12. * ①去掉验证中的textarea属性
  13. * ②根据maxlength或maxLength属性进行判断(文本框的最大长度属性应为maxLength)
  14. * ③根据validrule.cn来判断汉字,一个汉字为两个字符,且最大长度小于Form中对象的实际长度时才提示,等于最大长度时不提示
  15. * ④文本框也可以进行最大长度的验证,通过maxLength属性,最大限度防止字符串超常引起SQL语句错误
  16. *V2.1 马超修改内容
  17. * 1、加入“非汉字”的验证方式(validrule.nocn)
  18. * 2、加入“月-日”的验证方式(validrule.monthday)
  19. *V2.2 韩林平修改内容
  20. * 1、注释掉原2.0版本中一个汉字为两个字符的验证方式,原因:此验证方式不友好,用户还能录入但是实际已经超过录入上限。
  21. *V3.0 (2012-02-17)马超修改内容
  22. * 1、解决验证方法浏览器兼容性问题(Firefox等浏览器此验证函数永远返回true,等于无法进行验证,原因_elem.vmode在Firefox中无法获得vmode的值,原写法不规范只支持ie)
  23. * ①将所有获取对象自定义属性值的语句例如:从“_elem.vmode”改成“_elem.getAttribute("vmode")”
  24. * ②加入“getElement(strID)”方法用于获取对象
  25. *V3.1 (2012-04-25)杨彦彬修改内容
  26. * 1、添加doValidateInfo()校验方法,用于将全部不合格校验信息集中展示。保留原来逐条校验功能。
  27. *V3.2 (2012-07-12)马超修改内容
  28. * 1、修改float、double的验证正则表达式,原有正则表达式存在bug,原为“/^((([-\+]?\d+)(\.\d+))|(\.\d+)|(\d*))?$/”现改为“/^([-\+]?(((\d+)(\.\d+))|(\.\d+)|(\d*)))?$/”。原bug为输入“-11”或“-.5”会验证不过去。
  29. *V3.3 (2012-08-06)马超修改内容
  30. * 1、加入NoSingleQuotes的验证正则表达式,验证不含有单引号的字符串。
  31. *V3.4 (2013-01-14)杨飞修改内容
  32. * 1、加入decimal的验证正则表达式,小数点后最多3位 。
  33. * 2、加入decimalStr的验证正则表达式,验证输入的是符合如(15,2)格式的字符串。
  34. *
  35. */
  36. var validrule = new Object();
  37. validrule.chinese = /^([\u0391-\uFFE5|\s*]+$)?$/;
  38. validrule.cn = /[^\x00-\xff]/g //"是汉字的正则表达式
  39. validrule.nocn = /^([\x00-\xff]+$)?$/ //"是非汉字的正则表达式
  40. validrule.cnnum = /[^\u3447-\uFA29]/ig //"是汉字与数字的正则表达式
  41. validrule.english = /^([a-zA-Z|\s*]+)?$/;
  42. validrule.number = /^(\d*)?$/;
  43. validrule.decimal = /^((\d*)\.[0-9][0-9][0-9])?$/;
  44. validrule.integer = /^([-\+]?\d{1,9})?$/;
  45. validrule.float = /^([-\+]?(((\d+)(\.\d+))|(\.\d+)|(\d*)))?$/;
  46. validrule.double = /^([-\+]?(((\d+)(\.\d+))|(\.\d+)|(\d*)))?$/;
  47. validrule.string = /^([^'<>"]+)?$/;
  48. validrule.int = /^(\d{1,9})?$/;
  49. validrule.minusint = /^(\-([1-9])(\d*))?$/;
  50. validrule.date = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]))?$/;
  51. validrule.time = /^((0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))?$/;
  52. validrule.datetime = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]) (0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$/;
  53. validrule.datehm = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]) (0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]))?$/;
  54. validrule.year = /^(\d{4})?$/;
  55. validrule.month = /^([1-9]|0[1-9]|1[0-2])?$/;
  56. validrule.day = /^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])?$/;
  57. validrule.yearmonth = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2]))?$/;
  58. validrule.monthday = /^((0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]))?$/;
  59. validrule.postcode = /^(\d{6})?$/;
  60. validrule.email = /^(.+\@.+\..+)?$/;
  61. validrule.phone = /^(\(\d{3}\))?(\(?(\d{3}|\d{4}|\d{5})\)?(-?)(\d+))?((-?)(\d+))?$/;
  62. validrule.mobiletel = /^(01(\d{10})|1(\d{10}))?$/;
  63. validrule.ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  64. validrule.idcard = /^(\d{15}|\d{18}|\d{17}X|\d{17}x)?$/;
  65. validrule.tabledefine = /^(([A-Za-z])([A-Za-z0-9|_]){1,30})?$/;
  66. validrule.NumAndStr = /^([0-9a-zA-Z]+)?$/;
  67. validrule.LetterStr = /^([a-zA-Z]+)?$/;
  68. validrule.NumStr = /^(\d*)?$/;
  69. validrule.allstring = /^([^'"]+)?$/;
  70. validrule.NoSingleQuotes = /^([^']+)?$/;
  71. validrule.decimalStr = /^(([1-9]|1[0-5]),[1-2])?$/;
  72. var validInfo = "";
  73. var validInfoType = "";
  74. var validInfoArr = new Array();
  75. String.prototype.Trim = function()
  76. {
  77. return this.replace(/(^\s*)|(\s*$)/g, "");
  78. }
  79. String.prototype.LTrim = function()
  80. {
  81. return this.replace(/(^\s*)/g, "");
  82. }
  83. String.prototype.Rtrim = function()
  84. {
  85. return this.replace(/(\s*$)/g, "");
  86. }
  87. /**
  88. * 得到一个对象,解决多浏览器兼容性问题
  89. * strID 一般为对象的id属性也可以为对象的name属性(如果对象只有name属性则在Firefox中无法获取对象)
  90. */
  91. function getElement(strID){
  92. return (document.getElementById(strID)||document.getElementsByName(strID)[0]);
  93. }
  94. function doValidate( vform )
  95. {
  96. var elems = vform.elements;
  97. var frmLen = elems.length;
  98. var thePat = "";
  99. var strFormatInfo = "";
  100. //对于每一个FROM元素
  101. for(var i=0;i<frmLen;i++)
  102. {
  103. var _elem = elems[i];
  104. if(_elem.type=="application/x-shockwave-flash"){
  105. return true;
  106. }
  107. //为空检查
  108. if(_elem.getAttribute("vmode") != null && _elem.getAttribute("vmode") == "not null")
  109. {
  110. var strValue=_elem.value;
  111. strValue=strValue.Trim();
  112. if(strValue.length == 0)
  113. {
  114. layer.msg(_elem.getAttribute("vdisp")+"不能为空!");
  115. _elem.focus();
  116. return false;
  117. }
  118. }
  119. //类型检查
  120. if(_elem.getAttribute("vtype") == null)
  121. {
  122. continue;
  123. }
  124. if(_elem.getAttribute("vtype")=="none")
  125. {
  126. thePat = "";
  127. strFormatInfo = "";
  128. }
  129. if(_elem.getAttribute("vtype")=="chinese")
  130. {
  131. thePat = validrule.chinese;
  132. strFormatInfo = "中文";
  133. }
  134. if(_elem.getAttribute("vtype")=="nocn")
  135. {
  136. thePat = validrule.nocn;
  137. strFormatInfo = "非中文的字符串";
  138. }
  139. if(_elem.getAttribute("vtype")=="cnnum")
  140. {
  141. thePat = validrule.cnnum;
  142. strFormatInfo = "中文和数字的组合!";
  143. }
  144. if(_elem.getAttribute("vtype")=="english")
  145. {
  146. thePat = validrule.english;
  147. strFormatInfo = "英文字母";
  148. }
  149. if(_elem.getAttribute("vtype")=="number")
  150. {
  151. thePat = validrule.number;
  152. strFormatInfo = "阿拉伯数字";
  153. }
  154. if(_elem.getAttribute("vtype")=="integer")
  155. {
  156. thePat = validrule.integer;
  157. strFormatInfo = "整数";
  158. }
  159. if(_elem.getAttribute("vtype")=="float")
  160. {
  161. thePat = validrule.float;
  162. strFormatInfo = "浮点数";
  163. }
  164. if(_elem.getAttribute("vtype")=="double")
  165. {
  166. thePat = validrule.double;
  167. strFormatInfo = "实数";
  168. }
  169. if(_elem.getAttribute("vtype")=="string")
  170. {
  171. thePat = validrule.string;
  172. strFormatInfo = "不含特殊符号的字符串";
  173. }
  174. if(_elem.getAttribute("vtype")=="allstring")
  175. {
  176. thePat = validrule.allstring;
  177. strFormatInfo = "不含特殊符号的字符串";
  178. }
  179. if(_elem.getAttribute("vtype")=="NoSingleQuotes")
  180. {
  181. thePat = validrule.NoSingleQuotes;
  182. strFormatInfo = "不含单引号的字符串";
  183. }
  184. if(_elem.getAttribute("vtype")=="int")
  185. {
  186. thePat = validrule.int;
  187. strFormatInfo = "正整数";
  188. }
  189. if(_elem.getAttribute("vtype")=="minusint")
  190. {
  191. thePat = validrule.minusint;
  192. strFormatInfo = "负整数,比如 -123";
  193. }
  194. if(_elem.getAttribute("vtype")=="date")
  195. {
  196. thePat = validrule.date;
  197. strFormatInfo = "日期型,比如 2004-08-12";
  198. }
  199. if(_elem.getAttribute("vtype")=="time")
  200. {
  201. thePat = validrule.time;
  202. strFormatInfo = "时间型,比如 08:37:29";
  203. }
  204. if(_elem.getAttribute("vtype")=="datehm")
  205. {
  206. thePat = validrule.datehm;
  207. strFormatInfo = "日期时分型,比如 2004-08-12 12:25";
  208. }
  209. if(_elem.getAttribute("vtype")=="datetime")
  210. {
  211. thePat = validrule.datetime;
  212. strFormatInfo = "日期时间型,比如 2004-08-12 08:37:29";
  213. }
  214. if(_elem.getAttribute("vtype")=="yearmonth")
  215. {
  216. thePat = validrule.yearmonth;
  217. strFormatInfo = "年月型,比如 2011-01";
  218. }
  219. if(_elem.getAttribute("vtype")=="monthday")
  220. {
  221. thePat = validrule.monthday;
  222. strFormatInfo = "月日型,比如 08-12";
  223. }
  224. if(_elem.getAttribute("vtype")=="year")
  225. {
  226. thePat = validrule.year;
  227. strFormatInfo = "年代格式,比如 2005";
  228. }
  229. if(_elem.getAttribute("vtype")=="month")
  230. {
  231. thePat = validrule.month;
  232. strFormatInfo = "月份格式,比如 08";
  233. }
  234. if(_elem.getAttribute("vtype")=="day")
  235. {
  236. thePat = validrule.day;
  237. strFormatInfo = "日子格式,比如 14";
  238. }
  239. if(_elem.getAttribute("vtype")=="postcode")
  240. {
  241. thePat = validrule.postcode;
  242. strFormatInfo = "邮编,比如 100001";
  243. }
  244. if(_elem.getAttribute("vtype")=="email")
  245. {
  246. thePat = validrule.email;
  247. strFormatInfo = "电子邮件格式,比如 msm@hotmail.com";
  248. }
  249. if(_elem.getAttribute("vtype")=="phone")
  250. {
  251. thePat = validrule.phone;
  252. strFormatInfo = "电话号码格式,比如 010-67891234";
  253. }
  254. if(_elem.getAttribute("vtype")=="mobiletel")
  255. {
  256. thePat = validrule.mobiletel;
  257. strFormatInfo = "手机号码格式,比如 13867891234";
  258. }
  259. if(_elem.getAttribute("vtype")=="ip")
  260. {
  261. thePat = validrule.ip;
  262. strFormatInfo = "机器ip地址格式,比如 172.22.169.11";
  263. }
  264. if(_elem.getAttribute("vtype")=="idcard")
  265. {
  266. thePat = validrule.idcard;
  267. strFormatInfo = "身份证号码,比如 15位或者18位数字";
  268. }
  269. if(_elem.getAttribute("vtype")=="tabledefine")
  270. {
  271. thePat = validrule.tabledefine;
  272. strFormatInfo = "abc_defgf";
  273. }
  274. if(_elem.getAttribute("vtype")=="LetterStr")
  275. {
  276. thePat = validrule.LetterStr;
  277. strFormatInfo = "纯字母字符串";
  278. }
  279. if(_elem.getAttribute("vtype")=="NumAndStr")
  280. {
  281. thePat = validrule.NumAndStr;
  282. strFormatInfo = "数字或字母或数字和字母的组合";
  283. }
  284. if(_elem.getAttribute("vtype")=="NumStr")
  285. {
  286. thePat = validrule.NumStr;
  287. strFormatInfo = "纯数字组成的字符串";
  288. }
  289. if(_elem.getAttribute("vtype")=="decimalStr")
  290. {
  291. thePat = validrule.decimalStr;
  292. strFormatInfo = "定义decimal类型字段时,请输入如15,2格式数据";
  293. }
  294. var gotIt = null;
  295. if(thePat!="")
  296. {
  297. gotIt = thePat.exec((_elem.value).Trim());
  298. }
  299. if(gotIt == null)
  300. {
  301. layer.msg(_elem.getAttribute("vdisp")+"输入不合法,格式应为:"+strFormatInfo);
  302. _elem.focus();
  303. return false;
  304. }
  305. if(_elem.getAttribute("voperate")=="repeat")
  306. {
  307. if(_elem.value != getElement(_elem.getAttribute("to")).value)
  308. {
  309. layer.msg(_elem.getAttribute("msg"));
  310. _elem.focus();
  311. return false;
  312. }
  313. }
  314. if(_elem.getAttribute("voperate")=="rangeint")
  315. {
  316. if(parseInt(_elem.value) > parseInt(_elem.max) || parseInt(_elem.value) < parseInt(_elem.min))
  317. {
  318. layer.msg(_elem.getAttribute("msg"));
  319. _elem.focus();
  320. return false;
  321. }
  322. }
  323. if(_elem.getAttribute("voperate")=="rangestr")
  324. {
  325. if(_elem.value > _elem.max || _elem.value < _elem.min)
  326. {
  327. layer.msg(_elem.getAttribute("msg"));
  328. _elem.focus();
  329. return false;
  330. }
  331. }
  332. if(_elem.getAttribute("voperate")=="comparestr")
  333. {
  334. if(_elem.value <= getElement(_elem.getAttribute("to")).value)
  335. {
  336. layer.msg(_elem.getAttribute("msg"));
  337. _elem.focus();
  338. return false;
  339. }
  340. }
  341. if(_elem.getAttribute("voperate")=="largestr")
  342. {
  343. if(_elem.value <= getElement(_elem.getAttribute("to")).value)
  344. {
  345. layer.msg(_elem.getAttribute("msg"));
  346. _elem.focus();
  347. return false;
  348. }
  349. }
  350. if(_elem.getAttribute("voperate")=="largeequalstr")
  351. {
  352. if(_elem.value < getElement(_elem.getAttribute("to")).value)
  353. {
  354. layer.msg(_elem.getAttribute("msg"));
  355. _elem.focus();
  356. return false;
  357. }
  358. }
  359. if(_elem.getAttribute("voperate")=="compareint")
  360. {
  361. if(parseInt(_elem.value) <= parseInt(getElement(_elem.getAttribute("to")).value))
  362. {
  363. layer.msg(_elem.getAttribute("msg"));
  364. _elem.focus();
  365. return false;
  366. }
  367. }
  368. if(_elem.getAttribute("voperate")=="largeint")
  369. {
  370. if(parseInt(_elem.value) <= parseInt(getElement(_elem.getAttribute("to")).value))
  371. {
  372. layer.msg(_elem.getAttribute("msg"));
  373. _elem.focus();
  374. return false;
  375. }
  376. }
  377. if(_elem.getAttribute("voperate")=="largeequalint")
  378. {
  379. if(parseInt(_elem.value) < parseInt(getElement(_elem.getAttribute("to")).value))
  380. {
  381. layer.msg(_elem.getAttribute("msg"));
  382. _elem.focus();
  383. return false;
  384. }
  385. }
  386. if(_elem.getAttribute("voperate")=="extend")
  387. {
  388. if((_elem.value).lastIndexOf(_elem.getAttribute("extendname"))<=0)
  389. {
  390. layer.msg(_elem.getAttribute("msg"));
  391. _elem.focus();
  392. return false;
  393. }
  394. }
  395. if(_elem.getAttribute("voperate")=="custom")
  396. {
  397. if(!RegExp(_elem.getAttribute("regexp"),"g").test(_elem.value))
  398. {
  399. layer.msg(_elem.getAttribute("msg"));
  400. _elem.focus();
  401. return false;
  402. }
  403. }
  404. //检查Form中对象的最大长度
  405. if(_elem.getAttribute("maxlength")!=null || _elem.getAttribute("maxLength")!=null)
  406. {
  407. //"/[^\x00-\xff]/g"是汉字的正则表达式
  408. //strText.length+strText.length-strText.replace(/[^\x00-\xff]/g,"").length
  409. var ivaluelength=_elem.value.length;
  410. //var ivaluelength=_elem.value.replace(validrule.cn," ").length;
  411. var imaxlength;
  412. if(_elem.getAttribute("maxLength")!=null)
  413. {
  414. imaxlength=_elem.getAttribute("maxLength"); //文本框的最大长度属性应为maxLength
  415. }
  416. else{
  417. imaxlength=_elem.getAttribute("maxlength");
  418. }
  419. if(ivaluelength>imaxlength)
  420. {
  421. layer.msg(_elem.getAttribute("vdisp")+"输入的值长度太长超过了"+imaxlength+"个字符");
  422. _elem.focus();
  423. return false;
  424. }
  425. }
  426. //检查Form中对象的最小长度
  427. if(_elem.getAttribute("minlength")!=null || _elem.getAttribute("minLength")!=null)
  428. {
  429. //"/[^\x00-\xff]/g"是汉字的正则表达式
  430. //strText.length+strText.length-strText.replace(/[^\x00-\xff]/g,"").length
  431. var ivaluelength=_elem.value.length;
  432. //var ivaluelength=_elem.value.replace(validrule.cn," ").length;
  433. var iMinlength;
  434. if(_elem.getAttribute("maxLength")!=null)
  435. {
  436. iMinlength=_elem.getAttribute("minlength"); //文本框的最大长度属性应为maxLength
  437. }
  438. else{
  439. iMinlength=_elem.getAttribute("minLength");
  440. }
  441. if(ivaluelength<iMinlength)
  442. {
  443. layer.msg(_elem.getAttribute("vdisp")+"输入的值长度太少低于"+iMinlength+"个字符");
  444. _elem.focus();
  445. return false;
  446. }
  447. }
  448. }
  449. return true;
  450. }
  451. function doValidateInfo( vform )
  452. {
  453. var elems = vform.elements;
  454. var frmLen = elems.length;
  455. var thePat = "";
  456. var strFormatInfo = "";
  457. var iInfoArr = 0;
  458. //对于每一个FROM元素
  459. for(var i=0;i<frmLen;i++)
  460. {
  461. var _elem = elems[i];
  462. //为空检查
  463. if(_elem.getAttribute("vmode") != null && _elem.getAttribute("vmode") == "not null")
  464. {
  465. var strValue=_elem.value;
  466. strValue=strValue.Trim();
  467. if(strValue.length == 0)
  468. {
  469. validInfo+=" ["+_elem.getAttribute("vdisp")+"]\n";
  470. validInfoArr[iInfoArr++] = _elem;
  471. }
  472. }
  473. //类型检查
  474. if(_elem.getAttribute("vtype") == null)
  475. {
  476. continue;
  477. }
  478. if(_elem.getAttribute("vtype")=="none")
  479. {
  480. thePat = "";
  481. strFormatInfo = "";
  482. }
  483. if(_elem.getAttribute("vtype")=="chinese")
  484. {
  485. thePat = validrule.chinese;
  486. strFormatInfo = "中文";
  487. }
  488. if(_elem.getAttribute("vtype")=="nocn")
  489. {
  490. thePat = validrule.nocn;
  491. strFormatInfo = "非中文";
  492. }
  493. if(_elem.getAttribute("vtype")=="english")
  494. {
  495. thePat = validrule.english;
  496. strFormatInfo = "英文字母";
  497. }
  498. if(_elem.getAttribute("vtype")=="number")
  499. {
  500. thePat = validrule.number;
  501. strFormatInfo = "阿拉伯数字";
  502. }
  503. if(_elem.getAttribute("vtype")=="integer")
  504. {
  505. thePat = validrule.integer;
  506. strFormatInfo = "整数";
  507. }
  508. if(_elem.getAttribute("vtype")=="float")
  509. {
  510. thePat = validrule.float;
  511. strFormatInfo = "浮点数";
  512. }
  513. if(_elem.getAttribute("vtype")=="double")
  514. {
  515. thePat = validrule.double;
  516. strFormatInfo = "实数";
  517. }
  518. if(_elem.getAttribute("vtype")=="string")
  519. {
  520. thePat = validrule.string;
  521. strFormatInfo = "不含特殊符号的字符串";
  522. }
  523. if(_elem.getAttribute("vtype")=="allstring")
  524. {
  525. thePat = validrule.allstring;
  526. strFormatInfo = "不含特殊符号的字符串";
  527. }
  528. if(_elem.getAttribute("vtype")=="NoSingleQuotes")
  529. {
  530. thePat = validrule.NoSingleQuotes;
  531. strFormatInfo = "不含单引号的字符串";
  532. }
  533. if(_elem.getAttribute("vtype")=="int")
  534. {
  535. thePat = validrule.int;
  536. strFormatInfo = "正整数";
  537. }
  538. if(_elem.getAttribute("vtype")=="minusint")
  539. {
  540. thePat = validrule.minusint;
  541. strFormatInfo = "负整数,比如 -123";
  542. }
  543. if(_elem.getAttribute("vtype")=="date")
  544. {
  545. thePat = validrule.date;
  546. strFormatInfo = "日期型,比如 2004-08-12";
  547. }
  548. if(_elem.getAttribute("vtype")=="time")
  549. {
  550. thePat = validrule.time;
  551. strFormatInfo = "时间型,比如 08:37:29";
  552. }
  553. if(_elem.getAttribute("vtype")=="datehm")
  554. {
  555. thePat = validrule.datehm;
  556. strFormatInfo = "日期时分型,比如 2004-08-12 12:25";
  557. }
  558. if(_elem.getAttribute("vtype")=="datetime")
  559. {
  560. thePat = validrule.datetime;
  561. strFormatInfo = "日期时间型,比如 2004-08-12 08:37:29";
  562. }
  563. if(_elem.getAttribute("vtype")=="yearmonth")
  564. {
  565. thePat = validrule.yearmonth;
  566. strFormatInfo = "年月型,比如 2011-01";
  567. }
  568. if(_elem.getAttribute("vtype")=="monthday")
  569. {
  570. thePat = validrule.monthday;
  571. strFormatInfo = "月日型,比如 08-12";
  572. }
  573. if(_elem.getAttribute("vtype")=="year")
  574. {
  575. thePat = validrule.year;
  576. strFormatInfo = "年代格式,比如 2005";
  577. }
  578. if(_elem.getAttribute("vtype")=="month")
  579. {
  580. thePat = validrule.month;
  581. strFormatInfo = "月份格式,比如 08";
  582. }
  583. if(_elem.getAttribute("vtype")=="day")
  584. {
  585. thePat = validrule.day;
  586. strFormatInfo = "日子格式,比如 14";
  587. }
  588. if(_elem.getAttribute("vtype")=="postcode")
  589. {
  590. thePat = validrule.postcode;
  591. strFormatInfo = "邮编,比如 100001";
  592. }
  593. if(_elem.getAttribute("vtype")=="email")
  594. {
  595. thePat = validrule.email;
  596. strFormatInfo = "电子邮件格式,比如 msm@hotmail.com";
  597. }
  598. if(_elem.getAttribute("vtype")=="phone")
  599. {
  600. thePat = validrule.phone;
  601. strFormatInfo = "电话号码格式,比如 010-67891234";
  602. }
  603. if(_elem.getAttribute("vtype")=="mobiletel")
  604. {
  605. thePat = validrule.mobiletel;
  606. strFormatInfo = "手机号码格式,比如 13867891234";
  607. }
  608. if(_elem.getAttribute("vtype")=="ip")
  609. {
  610. thePat = validrule.ip;
  611. strFormatInfo = "机器ip地址格式,比如 172.22.169.11";
  612. }
  613. if(_elem.getAttribute("vtype")=="idcard")
  614. {
  615. thePat = validrule.idcard;
  616. strFormatInfo = "身份证号码,比如 15位或者18位数字";
  617. }
  618. if(_elem.getAttribute("vtype")=="tabledefine")
  619. {
  620. thePat = validrule.tabledefine;
  621. strFormatInfo = "abc_defgf";
  622. }
  623. if(_elem.getAttribute("vtype")=="LetterStr")
  624. {
  625. thePat = validrule.LetterStr;
  626. strFormatInfo = "纯字母字符串";
  627. }
  628. if(_elem.getAttribute("vtype")=="NumAndStr")
  629. {
  630. thePat = validrule.NumAndStr;
  631. strFormatInfo = "数字和字母字符串";
  632. }
  633. if(_elem.getAttribute("vtype")=="NumStr")
  634. {
  635. thePat = validrule.NumStr;
  636. strFormatInfo = "纯数字组成的字符串";
  637. }
  638. var gotIt = null;
  639. if(thePat!="")
  640. {
  641. gotIt = thePat.exec((_elem.value).Trim());
  642. }
  643. if(gotIt == null)
  644. {
  645. validInfoType +=" ["+_elem.getAttribute("vdisp")+"]输入不合法,格式应为:"+strFormatInfo+"\n";
  646. validInfoArr[iInfoArr++] = _elem;
  647. }
  648. if(_elem.getAttribute("voperate")=="repeat")
  649. {
  650. if(_elem.value != getElement(_elem.getAttribute("to")).value)
  651. {
  652. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  653. validInfoArr[iInfoArr++] = _elem;
  654. }
  655. }
  656. if(_elem.getAttribute("voperate")=="rangeint")
  657. {
  658. if(parseInt(_elem.value) > parseInt(_elem.max) || parseInt(_elem.value) < parseInt(_elem.min))
  659. {
  660. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  661. validInfoArr[iInfoArr++] = _elem;
  662. }
  663. }
  664. if(_elem.getAttribute("voperate")=="rangestr")
  665. {
  666. if(_elem.value > _elem.max || _elem.value < _elem.min)
  667. {
  668. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  669. validInfoArr[iInfoArr++] = _elem;
  670. }
  671. }
  672. if(_elem.getAttribute("voperate")=="comparestr")
  673. {
  674. if(_elem.value <= getElement(_elem.getAttribute("to")).value)
  675. {
  676. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  677. validInfoArr[iInfoArr++] = _elem;
  678. }
  679. }
  680. if(_elem.getAttribute("voperate")=="largestr")
  681. {
  682. if(_elem.value <= getElement(_elem.getAttribute("to")).value)
  683. {
  684. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  685. validInfoArr[iInfoArr++] = _elem;
  686. }
  687. }
  688. if(_elem.getAttribute("voperate")=="largeequalstr")
  689. {
  690. if(_elem.value < getElement(_elem.getAttribute("to")).value)
  691. {
  692. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  693. validInfoArr[iInfoArr++] = _elem;
  694. }
  695. }
  696. if(_elem.getAttribute("voperate")=="compareint")
  697. {
  698. if(parseInt(_elem.value) <= parseInt(getElement(_elem.getAttribute("to")).value))
  699. {
  700. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  701. validInfoArr[iInfoArr++] = _elem;
  702. }
  703. }
  704. if(_elem.getAttribute("voperate")=="largeint")
  705. {
  706. if(parseInt(_elem.value) <= parseInt(getElement(_elem.getAttribute("to")).value))
  707. {
  708. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  709. validInfoArr[iInfoArr++] = _elem;
  710. }
  711. }
  712. if(_elem.getAttribute("voperate")=="largeequalint")
  713. {
  714. if(parseInt(_elem.value) < parseInt(getElement(_elem.getAttribute("to")).value))
  715. {
  716. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  717. validInfoArr[iInfoArr++] = _elem;
  718. }
  719. }
  720. if(_elem.getAttribute("voperate")=="extend")
  721. {
  722. if((_elem.value).lastIndexOf(_elem.getAttribute("extendname"))<=0)
  723. {
  724. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  725. validInfoArr[iInfoArr++] = _elem;
  726. }
  727. }
  728. if(_elem.getAttribute("voperate")=="custom")
  729. {
  730. if(!RegExp(_elem.getAttribute("regexp"),"g").test(_elem.value))
  731. {
  732. validInfoType +=" ["+_elem.getAttribute("msg")+"]\n";
  733. validInfoArr[iInfoArr++] = _elem;
  734. }
  735. }
  736. //检查Form中对象的最大长度
  737. if(_elem.getAttribute("maxlength")!=null || _elem.getAttribute("maxLength")!=null)
  738. {
  739. //"/[^\x00-\xff]/g"是汉字的正则表达式
  740. //strText.length+strText.length-strText.replace(/[^\x00-\xff]/g,"").length
  741. var ivaluelength=_elem.value.length;
  742. //var ivaluelength=_elem.value.replace(validrule.cn," ").length;
  743. var imaxlength;
  744. if(_elem.getAttribute("maxLength")!=null)
  745. {
  746. imaxlength=_elem.getAttribute("maxLength"); //文本框的最大长度属性应为maxLength
  747. }
  748. else{
  749. imaxlength=_elem.getAttribute("maxlength");
  750. }
  751. if(ivaluelength>imaxlength)
  752. {
  753. validInfoType +=" ["+_elem.getAttribute("vdisp")+"]输入的值长度太长超过了"+imaxlength+"个字符\n";
  754. validInfoArr[iInfoArr++] = _elem;
  755. }
  756. }
  757. }
  758. if(validInfoType.length!=0 && validInfo.length!=0){
  759. layer.msg("不能为空的信息是\n"+validInfo+"\n类型错误的信息是\n"+validInfoType);
  760. validInfo="";
  761. validInfoType="";
  762. validInfoShow(validInfoArr);
  763. return false;
  764. }else if(validInfoType.length ==0 && validInfo.length!=0){
  765. layer.msg("不能为空的信息是\n"+validInfo);
  766. validInfo="";
  767. validInfoType="";
  768. validInfoShow(validInfoArr);
  769. return false;
  770. }else if(validInfoType.length!=0 && validInfo.length == 0){
  771. layer.msg("类型错误的信息是\n"+validInfoType);
  772. validInfo="";
  773. validInfoType="";
  774. validInfoShow(validInfoArr);
  775. return false;
  776. }
  777. return true;
  778. }
  779. function validInfoShow(){
  780. for(var i=0;i<validInfoArr.length;i++)
  781. {
  782. var _elem = validInfoArr[i];
  783. if(_elem.tagName == 'INPUT')
  784. {
  785. switch(_elem.type)
  786. {
  787. case 'text': _elem.style.backgroundColor='#FFFFCC';break;
  788. case 'file': _elem.style.backgroundColor='#FFFFCC';break;
  789. case 'checkbox': _elem.style.backgroundColor='#FFFFCC';break;
  790. }
  791. }else if(_elem.tagName == 'SELECT')
  792. {
  793. _elem.style.backgroundColor='#FFFFCC';
  794. }else if(_elem.tagName == 'TEXTAREA')
  795. {
  796. _elem.style.backgroundColor='#FFFFCC';
  797. }
  798. }
  799. }
  800. //以下为textarea标签限制最大长度功能
  801. function onmyinput(o,maxLength)
  802. {
  803. if(o.value.length>= maxLength)
  804. {
  805. if(o.value.length> maxLength)
  806. o.value = o.value.substring(0,maxLength);
  807. return false;
  808. }
  809. return true;
  810. }
  811. function mygetclipdata()
  812. {
  813. if(!document.all)
  814. {
  815. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  816. var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  817. var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
  818. trans.addDataFlavor('text/unicode');
  819. var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  820. clip.getData(trans,clip.kGlobalClipboard);
  821. var str=new Object();
  822. var strLength=new Object();
  823. trans.getTransferData("text/unicode",str,strLength);
  824. if (str)
  825. str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
  826. var pastetext;
  827. if (str)
  828. pastetext=str.data.substring(0,strLength.value / 2);
  829. return pastetext;
  830. }
  831. else
  832. {
  833. return window.clipboardData.getData("Text");
  834. }
  835. }
  836. function mysetclipdata(o)
  837. {
  838. if(!document.all)
  839. {
  840. netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  841. var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  842. var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
  843. trans.addDataFlavor("text/unicode");
  844. var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  845. str.data=o;
  846. trans.setTransferData("text/unicode",str,o.length*2);
  847. var clipid=Components.interfaces.nsIClipboard;
  848. clip.setData(trans,null,clipid.kGlobalClipboard);
  849. }
  850. else
  851. {
  852. window.clipboardData.setData("Text",o);
  853. }
  854. }
  855. function onmypaste(o, maxLength)
  856. {
  857. var nMaxLen=o.getAttribute? parseInt(maxLength):"";
  858. if(!document.all)
  859. {
  860. //layer.msg("不可能执行的代码");
  861. }
  862. else
  863. {
  864. if(document.selection.createRange().text.length>0)
  865. {
  866. var ovalueandclipboarddata = o.value +window.clipboardData.getData("Text");
  867. if(o.getAttribute && ovalueandclipboarddata.length-document.selection.createRange().text.length>nMaxLen)
  868. {
  869. if(window.clipboardData.getData("Text").substring(0,document.selection.createRange().text.length+nMaxLen-o.value.length)!="")
  870. window.clipboardData.setData("Text",window.clipboardData.getData("Text").substring(0,document.selection.createRange().text.length+nMaxLen-o.value.length));
  871. else
  872. return false;
  873. }
  874. }
  875. else
  876. {
  877. var ovalueandclipboarddata = o.value +window.clipboardData.getData("Text");
  878. if(o.getAttribute && ovalueandclipboarddata.length>nMaxLen)
  879. {
  880. if(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length)!="")
  881. window.clipboardData.setData("Text",ovalueandclipboarddata.substring(0,nMaxLen-o.value.length));
  882. else
  883. return false;
  884. }
  885. }
  886. return true;
  887. }
  888. }
  889. function onmykeypress(o, maxLength)
  890. {
  891. if(!document.all)
  892. {
  893. var nMaxLen=o.getAttribute? parseInt(maxLength):"";
  894. if(onmykeypress.caller.arguments[0].ctrlKey == true)
  895. {
  896. if(onmykeypress.caller.arguments[0].which==118)
  897. {
  898. if(o.selectionStart<o.selectionEnd)
  899. {
  900. var ovalueandclipboarddata = o.value + mygetclipdata();
  901. if(o.getAttribute && (ovalueandclipboarddata.length-o.selectionEnd + o.selectionStart>nMaxLen))
  902. {
  903. if(mygetclipdata().substring(0,o.selectionEnd - o.selectionStart+nMaxLen-o.value.length)!="")
  904. mysetclipdata(mygetclipdata().substring(0,o.selectionEnd - o.selectionStart+nMaxLen-o.value.length));
  905. else
  906. return false;
  907. }
  908. }
  909. else
  910. {
  911. var ovalueandclipboarddata = o.value +mygetclipdata();
  912. if(o.getAttribute && ovalueandclipboarddata.length>nMaxLen)
  913. {
  914. if(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length)!="")
  915. mysetclipdata(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length));
  916. else
  917. return false;
  918. }
  919. }
  920. return true;
  921. }
  922. }
  923. if(onmykeypress.caller.arguments[0].which==0 || onmykeypress.caller.arguments[0].which==8)
  924. return true;
  925. if(o.value.length>= maxLength)
  926. {
  927. if(o.selectionStart<o.selectionEnd)
  928. return true;
  929. if(o.value.length> maxLength)
  930. o.value = o.value.substring(0, maxLength);
  931. return false;
  932. }
  933. else
  934. return true;
  935. }
  936. else
  937. {
  938. if(document.selection.createRange().text.length>0)
  939. return true;
  940. if(o.value.length>= maxLength)
  941. return false;
  942. else
  943. return true;
  944. }
  945. }
  946. window.onload = init_MaxLength;
  947. function init_MaxLength ()
  948. {
  949. var textAreaObj = document.getElementsByTagName("textarea");
  950. var maxLength;
  951. for (var i = 0; i < textAreaObj.length; i++) {
  952. maxLength = textAreaObj[i].getAttribute("maxLength") == null ? 0 : textAreaObj[i].getAttribute("maxLength");
  953. if (maxLength == 0) continue;
  954. textAreaObj[i].onpaste = function(){ return onmypaste(this,maxLength)};
  955. textAreaObj[i].onkeypress = function(){ return onmykeypress(this,maxLength)};
  956. textAreaObj[i].onpropertychange = function(){ onmyinput(this,maxLength)};
  957. if(!document.all){
  958. textAreaObj[i].setAttribute("oninput","return onmyinput(this," + maxLength + ")");
  959. }
  960. }
  961. }
  962. /**
  963. * 日期格式化为字符串
  964. * @param fmt
  965. * @returns
  966. */
  967. Date.prototype.Format = function(fmt) {
  968. //补全3位
  969. function checkTime(i){
  970. if (i<100 && i>9){
  971. i = "0" + i;
  972. }
  973. if (i<10){
  974. i = "00" + i;
  975. }
  976. return i;
  977. }
  978. var o = {
  979. "M+" : this.getMonth() + 1, // 月份
  980. "d+" : this.getDate(), // 日
  981. "h+" : this.getHours(), // 小时
  982. "m+" : this.getMinutes(), // 分
  983. "s+" : this.getSeconds(), // 秒
  984. "q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
  985. "S" : checkTime(this.getMilliseconds())
  986. // 毫秒
  987. };
  988. if (/(y+)/.test(fmt))
  989. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
  990. .substr(4 - RegExp.$1.length));
  991. for ( var k in o)
  992. if (new RegExp("(" + k + ")").test(fmt))
  993. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
  994. : (("00" + o[k]).substr(("" + o[k]).length)));
  995. return fmt;
  996. }
  997. /**
  998. * 生成主键
  999. */
  1000. function getSequence(){
  1001. return (new Date()).Format("yyyyMMddhhmmssS")+parseInt(Math.random()*(999999-100000+1)+100000);
  1002. }
  1003. //去掉inpu里面的空格
  1004. function doTrim( vform )
  1005. {
  1006. var elems = vform.elements;
  1007. var frmLen = elems.length;
  1008. var thePat = "";
  1009. var strFormatInfo = "";
  1010. //对于每一个FROM元素
  1011. for(var i=0;i<frmLen;i++)
  1012. {
  1013. var _elem = elems[i];
  1014. if(_elem.type=="application/x-shockwave-flash"){
  1015. return true;
  1016. }
  1017. if(_elem.type=="file"){
  1018. return true;
  1019. }
  1020. //去掉空格
  1021. if (!(_elem.getAttribute("vselect")=='true')){
  1022. if(_elem.type=="text")
  1023. {
  1024. var strValue=_elem.value;
  1025. _elem.value=strValue.Trim()
  1026. }
  1027. }
  1028. }
  1029. }