PageRenderTime 56ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/mobile/WebContent/include/index/check.js

http://smartmobile.googlecode.com/
JavaScript | 477 lines | 377 code | 46 blank | 54 comment | 149 complexity | c026da51b9c5f388286e82cf74a73c8e MD5 | raw file
  1. /******************************************************************************
  2. * Copyright Š Mocha Software Co.,Ltd.
  3. ******************************************************************************/
  4. /*******************************************************************************
  5. * ???
  6. ******************************************************************************/
  7. function Verifier(){
  8. this.tools = new Tools();
  9. //????????
  10. this.checkNullField = function (Field){
  11. if(Field.value==""){
  12. //this.tools.showAlert('check.inputisnone');
  13. Field.focus();
  14. return false;
  15. }else{
  16. return true;
  17. }
  18. }
  19. //??????
  20. this.checkLawlessChar = function (Field){
  21. var srcStr = /[#<>"*/\:@;&*^#@$%:\"\'~+"/\;]/ig;
  22. var v=Field.value.match(srcStr);
  23. if(v !=null){
  24. this.tools.showAlert('check.includeillegalchar');
  25. Field.focus();
  26. return false;
  27. }
  28. else{
  29. return true;
  30. }
  31. }
  32. //????????????
  33. this.checkEnNumOnly = function (Field){
  34. if (Field.value != ""){
  35. var flag1 = false;
  36. var flag2 = false;
  37. for (i = 0; i < Field.value.length; i++){
  38. ch = Field.value.charAt(i);
  39. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z')) {
  40. this.tools.showAlert("check.EnNumOnly");
  41. Field.focus();
  42. return false;
  43. }
  44. if(ch >= '0' && ch <= '9'){//????
  45. flag1 = true;
  46. }
  47. if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
  48. flag2 = true;
  49. }
  50. }
  51. if(flag1 && flag2){
  52. return true;
  53. }else {
  54. this.tools.showAlert("check.EnNumOnly");
  55. return false;
  56. }
  57. }
  58. }
  59. //?????????????‘_’
  60. this.checkEnNumOnly2 = function (Field){
  61. if (Field.value != ""){
  62. var flag1 = false;
  63. var flag2 = false;
  64. var flag3 = false;
  65. for (i = 0; i < Field.value.length; i++){
  66. ch = Field.value.charAt(i);
  67. if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != '_')) {
  68. this.tools.showAlert("check.EnNumOnly2");
  69. Field.focus();
  70. return false;
  71. }
  72. if(ch >= '0' && ch <= '9'){//????
  73. flag1 = true;
  74. }
  75. if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
  76. flag2 = true;
  77. }
  78. }
  79. if(Field.value.indexOf('_') != -1){
  80. flag3 = true;
  81. }
  82. if(flag1 && flag2 && flag3){
  83. return true;
  84. }else {
  85. this.tools.showAlert("check.EnNumOnly2");
  86. return false;
  87. }
  88. }
  89. }
  90. //??URL????????
  91. this.checkLawlessCharURL = function (Field){
  92. var srcStr = /[#<>"*\@;&*^#@$%:\"\'~+"/\;]/ig;
  93. var url = "http://";
  94. if(Field.value.substring(0,7) == url){
  95. var v = Field.value.substring(7,Field.value.length).match(srcStr);
  96. if(v !=null){
  97. this.tools.showAlert('check.includeillegalchar');
  98. Field.focus();
  99. return false;
  100. }
  101. else{
  102. return true;
  103. }
  104. }else{
  105. this.tools.showAlert('check.includeillegalurl');
  106. return false;
  107. }
  108. }
  109. //???????????
  110. this.CheckInteger = function (Field){
  111. this.checkNullField(Field);
  112. if (Field.value != ""){
  113. for (i = 0; i < Field.value.length; i++){
  114. ch = Field.value.charAt(i);
  115. if ( (ch < '0' || ch > '9')/* && ch != '-' */) {
  116. //this.tools.showAlert("check.onlynumber");
  117. //Field.value="";
  118. Field.focus();
  119. return false;
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. //?????????????
  126. this.CheckReal = function (Field){
  127. this.checkNullField(Field);
  128. if (Field.value != ""){
  129. DotNum = 0;
  130. for (i = 0; i < Field.value.length; i++){
  131. ch = Field.value.charAt(i);
  132. if ((ch < '0' || ch > '9') && ch != '.'){
  133. this.tools.showAlert("check.onlynumber");
  134. Field.focus();
  135. return false;
  136. }
  137. if (ch == '.'){
  138. DotNum++;
  139. if (DotNum > 1){
  140. this.tools.showAlert("check.onlyonedot");
  141. Field.focus();
  142. return false;
  143. }
  144. }
  145. }
  146. }
  147. return true;
  148. }
  149. //???????Email??????
  150. this.CheckEmail1 = function (Field){
  151. // there must be >= 1 character before @, so we
  152. // start looking at character position 1
  153. // (i.e. second character)
  154. var i = 1;
  155. var len = Field.value.length;
  156. if (len > 50){
  157. this.tools.showAlert("check.emailformaterr");
  158. return false;
  159. }
  160. pos1 = Field.value.indexOf("@");
  161. pos2 = Field.value.indexOf(".");
  162. pos3 = Field.value.lastIndexOf("@");
  163. pos4 = Field.value.lastIndexOf(".");
  164. //check '@' and '.' is not first or last character
  165. if ((pos1 <= 0)||(pos1 == len-1)||(pos2 <= 0)||(pos2 == len-1)){
  166. this.tools.showAlert("check.illegalemail");
  167. Field.focus();
  168. return false;
  169. }
  170. else{
  171. //check @. or .@
  172. if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
  173. || ( pos1 != pos3 ) //find two @
  174. || ( pos4 < pos3 ) ) //. should behind the '@'
  175. {
  176. this.tools.showAlert("check.illegalemail");
  177. Field.focus();
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. // ?? E-mail ????
  184. this.CheckEmail = function (Field){
  185. // var e = document.getElementById("mailaddress").value;
  186. this.checkNullField(Field);
  187. var e = Field.value;
  188. if(e != "") {
  189. if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(e)) {
  190. this.tools.showAlert("check.illegalemail");
  191. // var email = document.getElementById ( "mailaddress" );
  192. // email.value = "";
  193. // email.focus ();
  194. Field.focus();
  195. return false;
  196. }
  197. }
  198. }
  199. //??IP??????
  200. this.isIPv4 = function (Field){
  201. // reg=/^(\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])$/;
  202. this.checkNullField(Field);
  203. var reg=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  204. if(!reg.test(Field.value)){
  205. this.tools.showAlert("check.illegalip");
  206. return false;
  207. }
  208. }
  209. ////??IP??????
  210. this.checkIPAddress = function (Field){
  211. this.checkNullField(Field);
  212. this.CheckIntegerForIPAddress(Field);
  213. var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  214. if(reSpaceCheck.test(Field.value)){
  215. Field.value.match(reSpaceCheck);
  216. if (RegExp.$1 <= 255 && RegExp.$1 >= 0
  217. && RegExp.$2 <= 255 && RegExp.$2 >= 0
  218. && RegExp.$3 <= 255 && RegExp.$3 >= 0
  219. && RegExp.$4 <= 255 && RegExp.$4 >= 0) {
  220. return true;
  221. }else{
  222. this.tools.showAlert("check.illegalip");
  223. return false;
  224. }
  225. }else{
  226. this.tools.showAlert("check.illegalip");
  227. return false;
  228. }
  229. }
  230. this.CheckIntegerForIPAddress = function (Field){
  231. if (Field.value != ""){
  232. for (i = 0; i < Field.value.length; i++){
  233. ch = Field.value.charAt(i);
  234. if ( (ch < '0' || ch > '9') && ch != '.' ) {
  235. this.tools.showAlert("check.illegalip");
  236. Field.value="";
  237. Field.focus();
  238. return false;
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. //?????????
  245. this.checkMobile = function(Field){
  246. var reg=/13[0,1,2,3,5,6,7,8,9]\d{8}/;
  247. if (Field.value.match(reg)== null) {
  248. this.tools.showAlert("check.illegalmobile");
  249. Field.focus();
  250. return false;
  251. }
  252. return true;
  253. }
  254. //????????
  255. this.checkChinese = function(Field){
  256. var reg = /^[\u4E00-\u9FA5\uF900-\uFA2D]+$/;
  257. var v = Field.value;
  258. if(v.match(reg) == null){
  259. this.tools.showAlert("chech.onlychinesechar");
  260. return false;
  261. }
  262. }
  263. //?????????'.'
  264. this.checkEnglish = function (Field){
  265. if (Field.value != ""){
  266. for (i = 0; i < Field.value.length; i++){
  267. ch = Field.value.charAt(i);
  268. if ( (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != '.')) {
  269. this.tools.showAlert("chech.onlyenglishchar");
  270. Field.focus();
  271. return false;
  272. }
  273. }
  274. }
  275. }
  276. //????????????
  277. this.checkContext = function (Field){
  278. if (Field.value != ""){
  279. if(Field.value.substring(0,1) != "/"){
  280. this.tools.showAlert("check.startwithbar");
  281. }else{
  282. for (i = 0; i < Field.value.length; i++){
  283. ch = Field.value.charAt(i);
  284. if ( (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != '/')) {
  285. this.tools.showAlert("chech.onlyenglishchar");
  286. Field.focus();
  287. return false;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. //?????????????'.'
  294. this.checkNotChinese = function (Field){
  295. if (Field.value != ""){
  296. var url = "http://";
  297. if(Field.value.substring(0,7) == url){
  298. for (i = 7; i < Field.value.length; i++){
  299. ch = Field.value.charAt(i);
  300. if ( (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != '.') && (ch < '0' || ch > '9')) {
  301. this.tools.showAlert("chech.onlyenglishchar");
  302. Field.focus();
  303. return false;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. this.checkNotChinese2 = function (Field){
  310. if (Field.value != ""){
  311. for (i = 0; i < Field.value.length; i++){
  312. ch = Field.value.charAt(i);
  313. if ( (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != '.') && (ch < '0' || ch > '9')) {
  314. this.tools.showAlert("chech.onlyenglishchar");
  315. Field.focus();
  316. return false;
  317. }
  318. }
  319. }
  320. }
  321. //????????
  322. this.checkBlankSpace = function (Field){
  323. var strlength;
  324. var k;
  325. var ch;
  326. strlength=Field.value.length;
  327. for(k=0;k<=strlength;k++){
  328. ch=Field.value.substring(k,k+1);
  329. if(ch==" "){
  330. this.tools.showAlert("check.nospace");
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. this.checkPage = function (Field){
  337. var str = Field.value;
  338. str = str.substring(str.indexOf("."),str.length);
  339. if(str != ".jsp" && str != ".html" && str != ".htm"){
  340. this.tools.showAlert("check.permissivefiletype");
  341. return false;
  342. }
  343. }
  344. //?????????
  345. /*
  346. this.checkPhone =function(){
  347. return /((^((\d){3,4}(\-)?)?(\d){7,8}$)|(^[1][3](\d){9,}$))|^[d]{1,}(\-)?(\d)$/.test(this);
  348. }
  349. */
  350. /***********************************************???***********************************************/
  351. //??????
  352. this.leftTrim = function (Field){
  353. return Field.value.replace(/(^\s*)/g, "");
  354. }
  355. //???????
  356. this.rightTrim = function (Field){
  357. return Field.value.replace(/(\s*$)/g, "");
  358. }
  359. //??????
  360. this.trim = function (Field){
  361. return Field.value.replace(/(^\s*)|(\s*$)/g, "");
  362. }
  363. //???????
  364. this.trimAll = function (Field){
  365. return Field.value.replace(/(\s*)/g, "");
  366. }
  367. //????????
  368. this.getLeft = function (Field,len){
  369. if(isNaN(len)|| len==null){
  370. len = Field.value.length;
  371. }else{
  372. if(parseInt(len)<0||parseInt(len)>Field.value.length){
  373. len = Field.value.length;
  374. }
  375. }
  376. return Field.value.substring(0,len);
  377. }
  378. //????????
  379. this.getRight = function (Field,len){
  380. if(isNaN(len)|| len==null){
  381. len = Field.value.length;
  382. }else{
  383. if(parseInt(len)<0||parseInt(len)>Field.value.length){
  384. len = Field.value.length;
  385. }
  386. }
  387. return Field.value.substring(Field.value.length-len,Field.value.length);
  388. }
  389. //????????
  390. this.getCurrentTime = function (){
  391. var now = new Date();
  392. var year = now.getYear();
  393. var month = now.getMonth();
  394. var date = now.getDate();
  395. var day = now.getDay();//??0~6,?~?
  396. var hours = now.getHours();
  397. var minutes = now.getMinutes();
  398. var seconds = now.getSeconds();
  399. var timeValue = year + "?"+month+"?"+date+"?"+switchWeekOfDay(day)+ hours;
  400. timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
  401. timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
  402. return timeValue;
  403. }
  404. //????,????
  405. function switchWeekOfDay(day){
  406. switch(day){
  407. case 0: return "???";
  408. case 1: return "???";
  409. case 2: return "???";
  410. case 3: return "???";
  411. case 4: return "???";
  412. case 5: return "???";
  413. case 6: return "???";
  414. }
  415. }
  416. //?????
  417. this.getDiffTime = function (date1,date2) {
  418. var diff=new Date()
  419. // sets difference date to difference of first date and second date
  420. diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
  421. timediff = diff.getTime();
  422. hours = Math.floor(timediff / (1000 * 60 * 60));
  423. timediff -= hours * (1000 * 60 * 60);
  424. mins = Math.floor(timediff / (1000 * 60));
  425. timediff -= mins * (1000 * 60);
  426. secs = Math.floor(timediff / 1000);
  427. timediff -= secs * 1000;
  428. return hours+":"+ mins+":"+secs;
  429. }
  430. }