近百个常用js代码汇总

本文提供了一系列JavaScript函数,用于验证各种格式的数据,包括日期、时间、数字、字符串等,并包含了身份证号码、电话号码等多种特定格式的验证逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1None.gif//檢查空串
  2ExpandedBlockStart.gifContractedBlock.giffunction isEmpty(str)dot.gif{
  3InBlock.gif if((str == null)||(str.length == 0)) return (true);
  4InBlock.gif else return(false);
  5ExpandedBlockEnd.gif}

  6None.gif
  7None.gif//檢查是否未數字
  8ExpandedBlockStart.gifContractedBlock.giffunction isDigit(theNum)dot.gif{   
  9InBlock.gifvar theMask = "0123456789";
 10InBlock.gifif (isEmpty(theNum)) return(false);
 11InBlock.gifelse if(theMask.indexOf(theNum) == -1return(false);
 12InBlock.gifreturn(true);
 13ExpandedBlockEnd.gif}

 14None.gif
 15None.gif//去掉左空格
 16ExpandedBlockStart.gifContractedBlock.giffunction trimLeft(str)dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gifif(str.charAt(0== " ")dot.gif{
 18InBlock.gifstr = str.slice(1);
 19InBlock.gifstr = trimLeft(str);
 20ExpandedSubBlockEnd.gif}

 21InBlock.gif return str;
 22ExpandedBlockEnd.gif}

 23None.gif
 24None.gif//去掉右空格
 25ExpandedBlockStart.gifContractedBlock.giffunction trimRight(str)dot.gif{
 26ExpandedSubBlockStart.gifContractedSubBlock.gifif(str.charAt( str.length - 1 ) == " ")dot.gif{
 27InBlock.gifstr = str.slice(0,str.length - 1);
 28InBlock.gifstr = trimRight(str);
 29ExpandedSubBlockEnd.gif}

 30InBlock.gif return str;
 31ExpandedBlockEnd.gif}

 32None.gif
 33None.gif//去掉左右空格
 34ExpandedBlockStart.gifContractedBlock.giffunction trim(str)dot.gif{
 35InBlock.gifreturn trimLeft(trimRight(str));
 36ExpandedBlockEnd.gif}

 37None.gif
 38None.gif//檢查是否是int
 39ExpandedBlockStart.gifContractedBlock.giffunction isInt(str)dot.gif{     
 40ExpandedSubBlockStart.gifContractedSubBlock.gifif(str=="")dot.gif{
 41InBlock.gifreturn (false);
 42ExpandedSubBlockEnd.gif}

 43ExpandedSubBlockStart.gifContractedSubBlock.gifelsedot.gif{
 44ExpandedSubBlockStart.gifContractedSubBlock.giffor(i=0;i<str.length;i++)dot.gif{
 45InBlock.gifvar chr = str.charAt(i);
 46ExpandedSubBlockStart.gifContractedSubBlock.gifif(!(chr>='0&& chr<='9'))dot.gif{
 47InBlock.gifreturn (false);
 48ExpandedSubBlockEnd.gif}

 49ExpandedSubBlockEnd.gif}

 50ExpandedSubBlockEnd.gif}

 51InBlock.gifreturn (true);
 52ExpandedBlockEnd.gif}

 53None.gif
 54None.gif//檢查str是小數,它的整數部分不多于i位,小數部分不多于j位
 55ExpandedBlockStart.gifContractedBlock.giffunction isDecimalNoMsg(str,i,j)dot.gif{           
 56InBlock.gifvar dot = str.indexOf(".");
 57InBlock.gifvar dot_last = str.lastIndexOf(".");
 58InBlock.gifvar str_f = "";
 59InBlock.gifvar str_b = "";
 60ExpandedSubBlockStart.gifContractedSubBlock.gifif ( dot != -1 )dot.gif{
 61InBlock.gifstr_f = str.substring(0,dot);
 62ExpandedSubBlockStart.gifContractedSubBlock.gif}
 else dot.gif{
 63InBlock.gifstr_f = str;
 64ExpandedSubBlockEnd.gif}

 65InBlock.gif
 66ExpandedSubBlockStart.gifContractedSubBlock.gifif ( dot_last != -1 )dot.gif{
 67InBlock.gifstr_b = str.substring(dot+1);
 68ExpandedSubBlockStart.gifContractedSubBlock.gif}
 else dot.gif{
 69InBlock.gifstr_b = str;
 70ExpandedSubBlockEnd.gif}

 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gifif( isInt( str_f ) == false )dot.gif{
 73InBlock.gif//alert(strMsg);
 74InBlock.gifreturn false;
 75ExpandedSubBlockStart.gifContractedSubBlock.gif}
 else if ( isInt( str_b ) == false )dot.gif{
 76InBlock.gif//alert(strMsg);
 77InBlock.gifreturn false;
 78ExpandedSubBlockStart.gifContractedSubBlock.gif}
 else if ( dot != dot_last )dot.gif{
 79InBlock.gif//alert(strMsg);
 80InBlock.gifreturn false;
 81ExpandedSubBlockStart.gifContractedSubBlock.gif}
 else if(dot==0 || dot_last==0)dot.gif{
 82InBlock.gif//alert(strMsg);
 83InBlock.gifreturn false;
 84ExpandedSubBlockEnd.gif}

 85InBlock.gif
 86ExpandedSubBlockStart.gifContractedSubBlock.gifif(str_f.length>i)dot.gif{
 87InBlock.gif//alert(strMsg);
 88InBlock.gifreturn false;
 89ExpandedSubBlockEnd.gif}

 90ExpandedSubBlockStart.gifContractedSubBlock.gifif(dot!=-1 && str_b.length>j)dot.gif{
 91InBlock.gif//alert(strMsg);
 92InBlock.gifreturn false;
 93ExpandedSubBlockEnd.gif}

 94InBlock.gifreturn true;
 95ExpandedBlockEnd.gif}

 96None.gif
 97None.gif//檢查日期格式Format: 99999999)
 98ExpandedBlockStart.gifContractedBlock.giffunction judgeDateFormat(dateStr)dot.gif{
 99InBlock.gif   var re,r;             
100ExpandedSubBlockStart.gifContractedSubBlock.gif   re = /\ddot.gif{8}/
101InBlock.gif   r = dateStr.match(re);           
102InBlock.gif  return(r);                     
103ExpandedBlockEnd.gif}

104None.gif
105None.gif//檢查時間格式(Format: 999999)
106ExpandedBlockStart.gifContractedBlock.giffunction judgeTimeFormat(timeStr)dot.gif{
107InBlock.gif   var re,r;             
108ExpandedSubBlockStart.gifContractedSubBlock.gif   re = /\ddot.gif{6}/
109InBlock.gif   r = timeStr.match(re);           
110InBlock.gif  return(r); 
111ExpandedBlockEnd.gif}

112None.gif   
113None.gif//檢查日期是否合法(Format: YYYYMMDD).
114ExpandedBlockStart.gifContractedBlock.giffunction validateDate(theStr)dot.gif{    
115ExpandedSubBlockStart.gifContractedSubBlock.gif if(theStr.length!=8)dot.gif{
116InBlock.gif return (false);
117ExpandedSubBlockStart.gifContractedSubBlock.gif }
 else dot.gif{
118InBlock.gif  if(theStr=="99999999")
119InBlock.gif return (true);
120InBlock.gif  var y = theStr.substring(0,4);
121InBlock.gif  var m = theStr.substring(4,6);
122InBlock.gif  var d = theStr.substring(6,8);
123InBlock.gif  var maxDays = 31;
124ExpandedSubBlockStart.gifContractedSubBlock.gif  if(isInt(m)==false||isInt(d)==false||isInt(y)==false)dot.gif{
125InBlock.gif   return(false);
126ExpandedSubBlockEnd.gif  }

127ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (y.length < 4)dot.gifreturn(false);}
128ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (!isBetween(m,1,12))dot.gifreturn(false);}
129ExpandedSubBlockStart.gifContractedSubBlock.gif  if(m.length!=2)dot.gifreturn(false);}
130InBlock.gif  else if (m==4||m==6||m==9||m==11)maxDays = 30;
131ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (m==2)dot.gif{
132InBlock.gif   if(y%4>0)maxDays = 28;
133InBlock.gif   else if(y%100==0&&y%400>0)maxDays = 28;
134InBlock.gif   else maxDays = 29;
135ExpandedSubBlockEnd.gif  }

136ExpandedSubBlockStart.gifContractedSubBlock.gif  if(isBetween(d,1,maxDays)==false)dot.gifreturn(false);}
137ExpandedSubBlockStart.gifContractedSubBlock.gif  if(d.length!=2)dot.gifreturn(false);}
138InBlock.gif  return(true);
139InBlock.gif
140ExpandedSubBlockEnd.gif }

141ExpandedBlockEnd.gif}

142None.gif
143None.gif//檢查時間是否合法(Format: HHMMSS).
144ExpandedBlockStart.gifContractedBlock.giffunction validateTime(theStr)dot.gif{    
145ExpandedSubBlockStart.gifContractedSubBlock.gif if(theStr.length!=6)dot.gif{
146InBlock.gif return (false);
147ExpandedSubBlockStart.gifContractedSubBlock.gif }
 else dot.gif{
148ExpandedSubBlockStart.gifContractedSubBlock.gif if(theStr == "240000")dot.gif{
149InBlock.gif return (true);
150ExpandedSubBlockEnd.gif }

151InBlock.gif  var h = theStr.substring(0,2);
152InBlock.gif  var m = theStr.substring(2,4);
153InBlock.gif  var s = theStr.substring(4,6);
154ExpandedSubBlockStart.gifContractedSubBlock.gif  if(isInt(h)==false||isInt(m)==false||isInt(s)==false)dot.gif{
155InBlock.gif   return(false);
156ExpandedSubBlockEnd.gif  }

157ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (h.length < 2)dot.gifreturn(false);}
158ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (!isBetween(h,0,23))dot.gifreturn(false);}
159ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (!isBetween(m,0,59))dot.gifreturn(false);}
160ExpandedSubBlockStart.gifContractedSubBlock.gif  else if (!isBetween(s,0,59))dot.gifreturn(false);}
161InBlock.gif  return true;
162ExpandedSubBlockEnd.gif  }

163ExpandedBlockEnd.gif}

164None.gif//檢查str包含漢字時最大長度不大于maxLen
165ExpandedBlockStart.gifContractedBlock.giffunction ChineseLenLimit( str, maxLen)dot.gif{
166InBlock.gif     var Strs = str;
167InBlock.gif     var strlength=0;
168InBlock.gif     var i;
169ExpandedSubBlockStart.gifContractedSubBlock.gif     for ( i=0;i<str.length;i++dot.gif{
170InBlock.gif        if(str.charCodeAt(i)>=1000)
171InBlock.gif                strlength += 2;
172InBlock.gif        else
173InBlock.gif                strlength += 1;
174ExpandedSubBlockEnd.gif     }

175ExpandedSubBlockStart.gifContractedSubBlock.gif     if ( strlength > maxLen )dot.gif{
176InBlock.gif     return false;
177ExpandedSubBlockEnd.gif     }

178InBlock.gif     return true;
179InBlock.gif
180ExpandedBlockEnd.gif}

181None.gif
182None.gif//檢查val在lo與hi之間
183ExpandedBlockStart.gifContractedBlock.giffunction isBetween(val,lo,hi)dot.gif
184ExpandedSubBlockStart.gifContractedSubBlock.gif if ((val < lo) || (val > hi))dot.gifreturn(false);}
185ExpandedSubBlockStart.gifContractedSubBlock.gif else dot.gifreturn(true);}
186ExpandedBlockEnd.gif}

187None.gif
188None.gif// 檢查charC是‘0’~‘9’
189ExpandedBlockStart.gifContractedBlock.giffunction chkChar(charC) dot.gif{
190ExpandedSubBlockStart.gifContractedSubBlock.gifif (charC == null || charC.length == 0dot.gif{
191InBlock.gifreturn false;
192ExpandedSubBlockEnd.gif}

193InBlock.gifif (charC == '0')return true;
194InBlock.gifif (charC == '1')return true;
195InBlock.gifif (charC == '2')return true;
196InBlock.gifif (charC == '3')return true;
197InBlock.gifif (charC == '4')return true;
198InBlock.gifif (charC == '5')return true;
199InBlock.gifif (charC == '6')return true;
200InBlock.gifif (charC == '7')return true;
201InBlock.gifif (charC == '8')return true;
202InBlock.gifif (charC == '9')return true;
203InBlock.gifreturn false;
204ExpandedBlockEnd.gif}

205None.gif
206None.gif// 檢查intI是正整數
207ExpandedBlockStart.gifContractedBlock.giffunction chkInt(intI) dot.gif{
208ExpandedSubBlockStart.gifContractedSubBlock.gifif (intI == null || intI.length == 0dot.gif{
209InBlock.gifreturn false;
210ExpandedSubBlockEnd.gif}

211ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=0;i<intI.length;i++dot.gif{
212ExpandedSubBlockStart.gifContractedSubBlock.gifif (!chkChar(intI.charAt(i))) dot.gif{
213InBlock.gifreturn false;
214ExpandedSubBlockEnd.gif}

215ExpandedSubBlockEnd.gif}

216ExpandedSubBlockStart.gifContractedSubBlock.gifif (intI.charAt(0== '0') dot.gif{
217InBlock.gifreturn false;
218ExpandedSubBlockEnd.gif}

219InBlock.gifreturn true;
220ExpandedBlockEnd.gif}

221None.gif// 檢查numN是數字
222ExpandedBlockStart.gifContractedBlock.giffunction chkNumber(numN) dot.gif{
223ExpandedSubBlockStart.gifContractedSubBlock.gifif (numN == null || numN.length == 0dot.gif{
224InBlock.gifreturn false;
225ExpandedSubBlockEnd.gif}

226ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=0;i<numN.length;i++dot.gif{
227ExpandedSubBlockStart.gifContractedSubBlock.gifif (!chkChar(numN.charAt(i))) dot.gif{
228InBlock.gifreturn false;
229ExpandedSubBlockEnd.gif}

230ExpandedSubBlockEnd.gif}

231ExpandedSubBlockStart.gifContractedSubBlock.gifif (numN.length > 1 && numN.charAt(0== '0') dot.gif{
232InBlock.gifreturn false;
233ExpandedSubBlockEnd.gif}

234InBlock.gifreturn true;
235ExpandedBlockEnd.gif}

236None.gif// 檢查錢數小數點后最多2位
237ExpandedBlockStart.gifContractedBlock.giffunction chkAmount(amtA) dot.gif{
238ExpandedSubBlockStart.gifContractedSubBlock.gifif (amtA == null || amtA.length == 0dot.gif{
239InBlock.gifreturn false;
240ExpandedSubBlockEnd.gif}

241InBlock.gifvar amtArray = new Array();
242InBlock.gifamtArray = amtA.split(".");
243ExpandedSubBlockStart.gifContractedSubBlock.gifif (amtArray.length > 2dot.gif{
244InBlock.gifreturn false;
245ExpandedSubBlockEnd.gif}

246ExpandedSubBlockStart.gifContractedSubBlock.gifif (amtArray.length == 1dot.gif{
247ExpandedSubBlockStart.gifContractedSubBlock.gifif (!chkNumber(amtArray[0])) dot.gif{
248InBlock.gifreturn false;
249ExpandedSubBlockEnd.gif}

250InBlock.gifreturn true;
251ExpandedSubBlockEnd.gif}

252ExpandedSubBlockStart.gifContractedSubBlock.gifif (!chkNumber(amtArray[0])) dot.gif{
253InBlock.gifreturn false;
254ExpandedSubBlockEnd.gif}

255ExpandedSubBlockStart.gifContractedSubBlock.gifif (amtArray[1].length > 2dot.gif{
256InBlock.gifreturn false;
257ExpandedSubBlockEnd.gif}

258ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=0;i<amtArray[1].length;i++dot.gif{
259ExpandedSubBlockStart.gifContractedSubBlock.gifif (!chkChar(amtArray[1].charAt(i))) dot.gif{
260InBlock.gifreturn false;
261ExpandedSubBlockEnd.gif}

262ExpandedSubBlockEnd.gif}

263InBlock.gifreturn true;
264ExpandedBlockEnd.gif}

265None.gif//檢查身份證
266ExpandedBlockStart.gifContractedBlock.giffunction checkAgentId(agentId)dot.gif{
267ExpandedSubBlockStart.gifContractedSubBlock.gif      if (agentId.length==10)dot.gif{
268ExpandedSubBlockStart.gifContractedSubBlock.gif        if (((agentId.charAt(0)=="A"&& (agentId.charAt(1)=="A"))||((agentId.charAt(0)=="A"&& (agentId.charAt(1)=="Z")))dot.gif{
269InBlock.gif           return true;
270ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
271InBlock.gif            checknum1=((agentId.charAt(0)>="A"&& (agentId.charAt(0)<="Z")); 
272InBlock.gif            checknum2=((agentId.charAt(1)==1|| (agentId.charAt(1)==2));
273ExpandedSubBlockStart.gifContractedSubBlock.gif            if (checknum2 && checknum1)dot.gif{
274InBlock.gif               id1 = agentId.charAt(0);
275ExpandedSubBlockStart.gifContractedSubBlock.gif               if (id1 == 'A') dot.gif{id0=1;}
276ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'B') dot.gif{ id0=10 ;}
277ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'C') dot.gif{ id0=19 ;}
278ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'D') dot.gif{ id0=28 ;}
279ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'E') dot.gif{ id0=37 ;}
280ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'F') dot.gif{ id0=46 ;}
281ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'G') dot.gif{ id0=55 ;}
282ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'H') dot.gif{ id0=64 ;}
283ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'I') dot.gif{ id0=39 ;}
284ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'J') dot.gif{ id0=73 ;}
285ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'K') dot.gif{ id0=82 ;}
286ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'L') dot.gif{ id0=2  ;}
287ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'M') dot.gif{ id0=11 ;}
288ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'N') dot.gif{ id0=20 ;}
289ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'O') dot.gif{ id0=48 ;}
290ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'P') dot.gif{ id0=29 ;}
291ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'Q') dot.gif{ id0=38 ;} 
292ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'R') dot.gif{ id0=47 ;}
293ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'S') dot.gif{ id0=56 ;}
294ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'T') dot.gif{ id0=65 ;}
295ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'U') dot.gif{ id0=74 ;}
296ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'V') dot.gif{ id0=83 ;}
297ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'W') dot.gif{ id0=21 ;}
298ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'X') dot.gif{ id0=3  ;}
299ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'Y') dot.gif{ id0=12 ;}
300ExpandedSubBlockStart.gifContractedSubBlock.gif               else if (id1 == 'Z') dot.gif{ id0=30 ;}
301InBlock.gif               id2 = id0 + agentId.charAt(1)*8 + agentId.charAt(2)*7 + agentId.charAt(3)*6 + agentId.charAt(4)*5 + agentId.charAt(5)*4 + agentId.charAt(6)*3 + agentId.charAt(7)*2 + agentId.charAt(8)*1 + agentId.charAt(9)*1;
302ExpandedSubBlockStart.gifContractedSubBlock.gif               if (id2 % 10 == 0)dot.gif{
303InBlock.gif                  return true;
304ExpandedSubBlockEnd.gif   }

305ExpandedSubBlockEnd.gif            }

306ExpandedSubBlockEnd.gif         }

307ExpandedSubBlockEnd.gif   }

308InBlock.gif   return false;
309ExpandedBlockEnd.gif}

310None.gif
311None.gif//轉換從UTF-8 到Big5 或 GB2312
312ExpandedBlockStart.gifContractedBlock.giffunction Unicode2Str(str)dot.gif{
313ExpandedSubBlockStart.gifContractedSubBlock.gifvar re=/&#[\da-fA-F]dot.gif{1,5};/ig;
314InBlock.gifvar arr=str.match(re);
315InBlock.gifif(arr==null)return("");
316InBlock.gifvar size=arr.length;
317InBlock.gifvar arr2=new Array(size);
318ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=0;i<arr.length;i++)dot.gif{
319InBlock.gifarr2[i]=String.fromCharCode(arr[i].replace(/[&#;]/g,""));
320ExpandedSubBlockEnd.gif}

321ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=0;i<arr.length;i++)dot.gif{
322InBlock.gifstr=str.replace(arr[i],arr2[i]);
323ExpandedSubBlockEnd.gif}

324InBlock.gif//return(arr.toString().replace(/,/g,""))
325InBlock.gifreturn str;
326ExpandedBlockEnd.gif}

327None.gif
328None.gif//獲得當前客戶端時間 (Format: YYYYMMDD).
329ExpandedBlockStart.gifContractedBlock.giffunction getCurrentDate()dot.gif{
330InBlock.gifvar dateObj= new Date();
331InBlock.gifvar dateString=dateObj.getFullYear();
332ExpandedSubBlockStart.gifContractedSubBlock.gifif(dateObj.getMonth()<10)dot.gif{
333InBlock.gifdateString=dateString+'0'+dateObj.getMonth();
334ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
335InBlock.gifdateString+=dateObj.getMonth();
336ExpandedSubBlockEnd.gif}

337ExpandedSubBlockStart.gifContractedSubBlock.gifif(dateObj.getDate()<10)dot.gif{
338InBlock.gifdateString+='0'+dateObj.getDate();
339ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
340InBlock.gifdateString+=dateObj.getDate();
341ExpandedSubBlockEnd.gif}

342InBlock.gifreturn dateString;
343ExpandedBlockEnd.gif}
   
344None.gif
345None.gif//居中顯示彈出窗口
346ExpandedBlockStart.gifContractedBlock.giffunction PopWindowOnCenter(url,title,iwidth,iheight)dot.gif{
347InBlock.gifvar ileft,itop;
348InBlock.gifileft = (window.screen.width-iwidth)/2;
349InBlock.gifitop = (window.screen.height-iheight)/2;
350InBlock.gif
351InBlock.gifwindow.open(url,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+iwidth+',height='+iheight+',left='+ileft+',top='+itop);
352ExpandedBlockEnd.gif}

353None.gif
354None.gif//移動選中的成員從objFromId的select到objToId的select
355ExpandedBlockStart.gifContractedBlock.giffunction moveTo(objFromId,objToId)dot.gif{
356InBlock.gifvar objFrom = document.getElementById(objFromId);
357InBlock.gifvar objTo = document.getElementById(objToId);
358InBlock.gif
359InBlock.gifvar len = objFrom.length;
360ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=len-1; i>=0; i--)dot.gif{
361ExpandedSubBlockStart.gifContractedSubBlock.gifif(objFrom.options[i].selected)dot.gif{
362InBlock.gifvar j = 0;
363ExpandedSubBlockStart.gifContractedSubBlock.giffor(j = 0; j < objTo.length; j++)dot.gif{
364ExpandedSubBlockStart.gifContractedSubBlock.gifif(objFrom.options[i].value == objTo.options[j].value)dot.gif{
365InBlock.gifbreak;
366ExpandedSubBlockEnd.gif}

367ExpandedSubBlockEnd.gif}

368ExpandedSubBlockStart.gifContractedSubBlock.gifif(j == objTo.length)dot.gif{//the selected item is not in objTo.
369InBlock.gifobjFrom.options[i].selected = false;
370InBlock.gifvar option1 = objFrom.options[i];
371InBlock.gifobjTo.options.add(new Option(option1.text, option1.value));
372InBlock.gifobjFrom.options.remove(i);
373ExpandedSubBlockEnd.gif}

374InBlock.gifobjFrom = document.getElementById(objFromId);
375InBlock.giflen = objFrom.length;
376ExpandedSubBlockEnd.gif}

377ExpandedSubBlockEnd.gif}

378ExpandedBlockEnd.gif}

379None.gif
380None.gif//移動全部成員從objFromId的select到objToId的select
381ExpandedBlockStart.gifContractedBlock.giffunction moveAllTo(objFromId,objToId)dot.gif{
382InBlock.gifvar objFrom = document.getElementById(objFromId);
383InBlock.gifvar objTo = document.getElementById(objToId);
384InBlock.gif
385InBlock.gifvar len = objFrom.length;
386ExpandedSubBlockStart.gifContractedSubBlock.giffor(var i=len-1; i>=0; i--)dot.gif{
387InBlock.gifvar j = 0;
388ExpandedSubBlockStart.gifContractedSubBlock.giffor(j = 0; j < objTo.length; j++)dot.gif{
389ExpandedSubBlockStart.gifContractedSubBlock.gifif(objFrom.options[i].value == objTo.options[j].value)dot.gif{
390InBlock.gifbreak;
391ExpandedSubBlockEnd.gif}

392ExpandedSubBlockEnd.gif}

393ExpandedSubBlockStart.gifContractedSubBlock.gifif(j == objTo.length)dot.gif{//the selected item is not in objTo.
394InBlock.gifvar option1 = objFrom.options[i];
395InBlock.gifobjTo.options.add(new Option(option1.text, option1.value));
396InBlock.gifobjFrom.options.remove(i);
397ExpandedSubBlockEnd.gif}

398InBlock.gifobjFrom = document.getElementById(objFromId);
399InBlock.giflen = objFrom.length;
400ExpandedSubBlockEnd.gif}

401ExpandedBlockEnd.gif}

402None.gif
403None.gif
404None.gif//屏蔽非數字鍵
405ExpandedBlockStart.gifContractedBlock.giffunction checkKey()dot.gif{
406InBlock.gifif(event.keyCode<48 || (event.keyCode>57 && event.keyCode<96)
407ExpandedSubBlockStart.gifContractedSubBlock.gif|| event.keyCode>105)dot.gif{
408ExpandedSubBlockStart.gifContractedSubBlock.gifif(event.keyCode != 8)dot.gif{
409InBlock.gifevent.returnValue=false;
410ExpandedSubBlockEnd.gif}

411ExpandedSubBlockEnd.gif}

412ExpandedBlockEnd.gif}

413None.gif
414None.gif//用與樹狀列表的顯示
415ExpandedBlockStart.gifContractedBlock.giffunction showMenu(id,ulId)dot.gif{
416InBlock.gifvar obj = document.getElementById(id);
417InBlock.gif
418ExpandedSubBlockStart.gifContractedSubBlock.gifif(obj.className == "")dot.gif{
419InBlock.gifobj.className = "selected";
420InBlock.gifdocument.getElementById(ulId).style.display = "block";
421ExpandedSubBlockEnd.gif}

422ExpandedSubBlockStart.gifContractedSubBlock.gifelsedot.gif{
423InBlock.gifobj.className = "";
424InBlock.gifdocument.getElementById(ulId).style.display = "none";
425ExpandedSubBlockEnd.gif}

426ExpandedBlockEnd.gif}

427None.gif
428None.gif//檢查str中是否包含漢字
429ExpandedBlockStart.gifContractedBlock.giffunction CheckChinese( str)dot.gif{
430InBlock.gif     var Strs = str;
431InBlock.gif     var i;
432ExpandedSubBlockStart.gifContractedSubBlock.gif     for ( i=0;i<str.length;i++dot.gif{
433ExpandedSubBlockStart.gifContractedSubBlock.gif        if(str.charCodeAt(i)>=1000)dot.gif{
434InBlock.gif                return true;
435ExpandedSubBlockEnd.gif        }

436ExpandedSubBlockEnd.gif     }

437InBlock.gif     return false;
438ExpandedBlockEnd.gif}

439None.gif
440None.gif//校验是否全由数字组成 
441None.gif
442None.gif程序代码 
443None.giffunction isDigit(s) 
444ExpandedBlockStart.gifContractedBlock.gifdot.gif
445ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[0-9]dot.gif{1,20}$/
446InBlock.gifif (!patrn.exec(s)) return false 
447InBlock.gifreturn true 
448ExpandedBlockEnd.gif}
 
449None.gif
450None.gif//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串 
451None.gif
452None.gif程序代码 
453None.giffunction isRegisterUserName(s) 
454ExpandedBlockStart.gifContractedBlock.gifdot.gif
455ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[a-zA-Z]dot.gif{1}([a-zA-Z0-9]|[._])dot.gif{4,19}$/
456InBlock.gifif (!patrn.exec(s)) return false 
457InBlock.gifreturn true 
458ExpandedBlockEnd.gif}
 
459None.gif
460None.gif//校验用户姓名:只能输入1-30个以字母开头的字串 
461None.gif
462None.gif程序代码 
463None.giffunction isTrueName(s) 
464ExpandedBlockStart.gifContractedBlock.gifdot.gif
465ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[a-zA-Z]dot.gif{1,30}$/
466InBlock.gifif (!patrn.exec(s)) return false 
467InBlock.gifreturn true 
468ExpandedBlockEnd.gif}
 
469None.gif
470None.gif//校验密码:只能输入6-20个字母、数字、下划线 
471None.gif
472None.gif程序代码 
473None.giffunction isPasswd(s) 
474ExpandedBlockStart.gifContractedBlock.gifdot.gif
475ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^(w)dot.gif{6,20}$/
476InBlock.gifif (!patrn.exec(s)) return false 
477InBlock.gifreturn true 
478ExpandedBlockEnd.gif}
 
479None.gif
480None.gif//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-” 
481None.gif
482None.gif程序代码 
483None.giffunction isTel(s) 
484ExpandedBlockStart.gifContractedBlock.gifdot.gif
485InBlock.gif//var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?(d){1,12})+$/; 
486ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[+]dot.gif{0,1}(d)dot.gif{1,3}[ ]?([-]?((d)|[ ])dot.gif{1,12})+$/
487InBlock.gifif (!patrn.exec(s)) return false 
488InBlock.gifreturn true 
489ExpandedBlockEnd.gif}
 
490None.gif
491None.gif//校验手机号码:必须以数字开头,除数字外,可含有“-” 
492None.gif
493None.gif程序代码 
494None.giffunction isMobil(s) 
495ExpandedBlockStart.gifContractedBlock.gifdot.gif
496ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[+]dot.gif{0,1}(d)dot.gif{1,3}[ ]?([-]?((d)|[ ])dot.gif{1,12})+$/
497InBlock.gifif (!patrn.exec(s)) return false 
498InBlock.gifreturn true 
499ExpandedBlockEnd.gif}
 
500None.gif
501None.gif//校验邮政编码 
502None.gif
503None.gif程序代码 
504None.giffunction isPostalCode(s) 
505ExpandedBlockStart.gifContractedBlock.gifdot.gif
506InBlock.gif//var patrn=/^[a-zA-Z0-9]{3,12}$/; 
507ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[a-zA-Z0-9 ]dot.gif{3,12}$/
508InBlock.gifif (!patrn.exec(s)) return false 
509InBlock.gifreturn true 
510ExpandedBlockEnd.gif}
 
511None.gif
512None.gif//校验搜索关键字 
513None.gif
514None.gif程序代码 
515None.giffunction isSearch(s) 
516ExpandedBlockStart.gifContractedBlock.gifdot.gif
517ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[^`~!@#$%^&*()+=|][]dot.gif{}:;',.<>/?]dot.gif{1}[^`~!@$%^&()+=|][]dot.gif{}:;',.<>?]dot.gif{0,19}$/
518InBlock.gifif (!patrn.exec(s)) return false 
519InBlock.gifreturn true 
520ExpandedBlockEnd.gif}
 
521None.gif
522None.gif程序代码 
523None.giffunction isIP(s) //by zergling 
524ExpandedBlockStart.gifContractedBlock.gifdot.gif
525ExpandedSubBlockStart.gifContractedSubBlock.gifvar patrn=/^[0-9.]dot.gif{1,20}$/
526InBlock.gifif (!patrn.exec(s)) return false 
527InBlock.gifreturn true 
528ExpandedBlockEnd.gif}

529None.gif
530ExpandedBlockStart.gifContractedBlock.gif/**//**
531InBlock.gif * 功能:使指定值的指定名称的单复选框处于选中状态。
532InBlock.gif * radioName:单选框组件名
533InBlock.gif * val:指定值
534ExpandedBlockEnd.gif */

535ExpandedBlockStart.gifContractedBlock.giffunction makeRadioChecked(radioName, val) dot.gif{
536InBlock.gif    var obj = document.all[radioName];
537ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
538ExpandedSubBlockStart.gifContractedSubBlock.gif        if (obj) dot.gif{
539ExpandedSubBlockStart.gifContractedSubBlock.gif            if (obj.type == "radio" && obj.value == val) dot.gif{
540InBlock.gif                obj.checked = true;
541ExpandedSubBlockEnd.gif            }

542ExpandedSubBlockStart.gifContractedSubBlock.gif            for (var i = 0; i < obj.length; i++dot.gif{
543ExpandedSubBlockStart.gifContractedSubBlock.gif                if (obj[i].type == "radio" && obj[i].value == val) dot.gif{
544InBlock.gif                    obj[i].checked = true;
545InBlock.gif                    break;
546ExpandedSubBlockEnd.gif                }

547ExpandedSubBlockEnd.gif            }

548ExpandedSubBlockEnd.gif        }

549ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch(exception) dot.gif{
550InBlock.gif        alert("error");
551ExpandedSubBlockEnd.gif    }

552ExpandedBlockEnd.gif}

553None.gif
554None.gif自创javascrit分页代码。
555None.gif
556ExpandedBlockStart.gifContractedBlock.giffunction showpages(total,perpage,current,filename,seed,bShow)dot.gif{
557InBlock.gif//total总记录数,perpage每页记录数,current当前记录,filename文件名?page=,seed中间数字两边间隔数,bshow显示中间数字页面
558InBlock.gif//
559InBlock.gif
560InBlock.gif
561InBlock.gifvar sRet,i,startPage,endPage,totalPage
562InBlock.gif//startPage:循环开始/endPage:循环结束/totalPage:总页数
563InBlock.gif//处理URL中的空格
564ExpandedSubBlockStart.gifContractedSubBlock.gifif (filename!='')dot.gif{
565InBlock.giffilename="&"+filename;
566ExpandedSubBlockEnd.gif}

567ExpandedSubBlockStart.gifContractedSubBlock.gif  if (total % perpage==0 )dot.gif{
568InBlock.gif
569InBlock.gif totalPage=total/perpage;
570InBlock.gif
571ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
572InBlock.gif
573InBlock.giftotalPage=Math.floor(total/perpage)+1;
574ExpandedSubBlockEnd.gif}

575InBlock.gif
576InBlock.gif
577ExpandedSubBlockStart.gifContractedSubBlock.gif  if (totalPage<=10)dot.gif{
578InBlock.gifstartPage=1;
579ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
580ExpandedSubBlockStart.gifContractedSubBlock.gifif ((current-seed) >0dot.gif{
581InBlock.gifstartPage=current-seed;
582ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
583InBlock.gifstartPage=1;
584ExpandedSubBlockEnd.gif}

585ExpandedSubBlockEnd.gif}

586InBlock.gif
587ExpandedSubBlockStart.gifContractedSubBlock.gifif (totalPage<=10dot.gif{
588InBlock.gifendPage=totalPage
589ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
590ExpandedSubBlockStart.gifContractedSubBlock.gifif ((current+seed)<totalPage) dot.gif{
591InBlock.gifendPage=current+seed
592ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
593InBlock.gifendPage=totalPage
594ExpandedSubBlockEnd.gif}

595ExpandedSubBlockEnd.gif}

596InBlock.gif
597ExpandedSubBlockStart.gifContractedSubBlock.gifif (current<seed) dot.gif{
598ExpandedSubBlockStart.gifContractedSubBlock.gifif (totalPage>10)dot.gif{
599InBlock.gifendPage=10
600ExpandedSubBlockEnd.gif}

601ExpandedSubBlockEnd.gif}

602InBlock.gif var sRet1=""
603InBlock.gif
604ExpandedSubBlockStart.gifContractedSubBlock.gif if (bShow) dot.gif{
605InBlock.giffor (i=startPage;i<=endPage;i++)
606ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
607InBlock.gif
608InBlock.gifif (i==current) 
609ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
610InBlock.gifsRet1=sRet1+"<font style='background-color:#CCCC33;'><b>"+current+"</b></font> "
611ExpandedSubBlockEnd.gif}

612InBlock.gifelse
613ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
614InBlock.gifsRet1=sRet1+"<a href=?page="+i+filename+">"+i+"</a> "
615ExpandedSubBlockEnd.gif}

616InBlock.gif
617ExpandedSubBlockEnd.gif}

618ExpandedSubBlockEnd.gif }

619InBlock.gif
620InBlock.gif    sRet=""
621InBlock.gif    //sRet=sRet+"<form name=jumpPage mothod=post action= >"
622InBlock.gifsRet=sRet+"<font class=tcat2>共"+total+"条,"
623InBlock.gif    sRet=sRet+""+current+"页/共"+ totalPage+"页, "
624InBlock.gifsRet=sRet+"<a href=?page=1"+ filename+">第一页</a> "
625ExpandedSubBlockStart.gifContractedSubBlock.gifif ((current==1)&&(current!=totalPage))dot.gif{
626InBlock.gif
627InBlock.gif
628InBlock.gifsRet=sRet+" 上一页 "+sRet1+" <a href=?page="+(current+1+filename+">下一页</a>"
629ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
630ExpandedSubBlockStart.gifContractedSubBlock.gifif (current>1dot.gif{
631InBlock.gif
632ExpandedSubBlockStart.gifContractedSubBlock.gifif  (current<totalPage) dot.gif{
633InBlock.gifsRet=sRet+" <a href=?page="+(current-1)+filename+">上一页</a> "+sRet1+" <a href=?page="+(current+1)+filename+">下一页</a>"
634ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
635ExpandedSubBlockStart.gifContractedSubBlock.gifif (current==totalPage) dot.gif{
636InBlock.gifsRet=sRet+" <a href=?page="+(current-1)+filename+">上一页</a> "+sRet1+" 下一页"
637InBlock.gif
638ExpandedSubBlockEnd.gif}

639ExpandedSubBlockEnd.gif    }

640ExpandedSubBlockStart.gifContractedSubBlock.gif}
elsedot.gif{
641InBlock.gifsRet=sRet+" 上一页 "+sRet1+" 下一页"
642ExpandedSubBlockEnd.gif}

643ExpandedSubBlockEnd.gif}

644InBlock.gifsRet=sRet+"  <a href=?page="+ totalPage+filename+">最末页</a>"
645InBlock.gif//sRet=sRet+"<input type=hidden name=wheretogo value=go>&nbsp;"
646InBlock.gif//sRet=sRet+"<input type=hidden name=maxpage value="+totalPage+">"
647InBlock.gifsRet=sRet+"  跳转到<input name=currentPage class=border1px size=3  onkeydown=if((event.keyCode==13)&&(this.value!='')&&(this.value!=0)&&(this.value<"+(totalPage+1)+"))window.location='?page='+this.value+'"+filename+"'; onkeyup=if(isNaN(this.value))this.value=''; >页 "
648InBlock.gif//sRet=sRet+"<input type=button value=GO class=border1px onclick=jump('"+filename+"');>&nbsp;"
649InBlock.gif
650InBlock.gifsRet=sRet+"</font>"
651InBlock.gif//sRet=sRet+"</form>"
652InBlock.gif//alert(sRet);
653InBlock.gifdocument.write(sRet)
654InBlock.gif
655ExpandedBlockEnd.gif}

656None.gif
657None.gif检查有没有空格
658ExpandedBlockStart.gifContractedBlock.gifif(aa.indexOf(" ")>=0)dot.gif{
659InBlock.gif   alert("aa中不允许含有空格!!");
660InBlock.gif   document.myForm.aa.focus();
661InBlock.gif   return false;
662ExpandedBlockEnd.gif}

663None.gif

转载于:https://www.cnblogs.com/yongning/archive/2007/08/14/854827.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值