ÿþ//==================================================================================================== // [ÒcöN Tðy] jQuery DateTimeMask //---------------------------------------------------------------------------------------------------- // [Ïc ð] jQuery DateTimeMaskåeg©cxÒcöN ÿƒ[/fúWŽNjQuery{|“^ ÿž[°s†Njs,gŽNu˜b—„vR»y0ù[N*NUSLˆ // ‡e,gFh ÿ`OêS‰—‰™Qÿ$("input_id").DateTimeMask();1\ý€Œ[Ž„vž[°s“eQ§c6R ÿîvMRý€ž[°s5Íyåe // g©cx§c6R0(Wie6.0ŒTfirefox3.0 NŒÕ‹Ç0,gÒcöNǑ(uM‘náOo`„v`ó` ÿ`OïSåN{~gibU\̑b—„v // ŸRý€ ÿÎN €&{Têñ]„vN¡R;‘—‰ //---------------------------------------------------------------------------------------------------- // [\O€Q T] +s¬Q // [åe g] 2008-02-04 // [® ±{] wzmaodong@126.com // [\O€ZS¢[] http://wzmaodong.cnblogs.com //==================================================================================================== $.fn.DateTimeMask = function(settings){ var options = { safemodel: true, //‰[hQ!j_ N ÿ Ný€˜|4 ÿ Ný€ÖbÉb masktype: "3", //1:yyyy-MM-dd HH:mm:ss 2:yyyy-MM-dd HH:mm 3:yyyy-MM-dd 4:HH:mm 5:HH:mm:ss isnull: false, ///f&TïSåNhQèý/f0 lawlessmessage: "`O“eQ„v 0) { fronttext = strText.substr(nCursorPos - 1,1); if(fronttext!="-" && fronttext!=":" && fronttext!=" ") { fronttext="0"; strText = strText.substr(0,nCursorPos - 1) + fronttext + strText.substr(nCursorPos, nTextLen-nCursorPos); } nCursorPos--; } break; case KEY.DEL: if(nCursorPos<nTextLen) { behindtext = strText.substr(nCursorPos,1); if(behindtext!="-" && behindtext!=":" && behindtext!=" ") behindtext="0"; if(nCursorPos + 1 == nTextLen) strText = strText.substr(0,nCursorPos) + behindtext; else strText = strText.substr(0,nCursorPos) + behindtext + strText.substr(nCursorPos+1,nTextLen-nCursorPos-1); nCursorPos++; } break; default : if(nCursorPos==nTextLen) break; if(!(nKeyCode >=48 && nKeyCode<=57 || nKeyCode >=96 && nKeyCode<=105)) break; if (nKeyCode > 95) nKeyCode -= (95-47); behindtext = strText.substr(nCursorPos,1); if(behindtext!="-" && behindtext!=":" && behindtext!=" ") { var keycode = String.fromCharCode(nKeyCode); preText = strText.substr(0,nCursorPos) + keycode + strText.substr(nCursorPos+1,nTextLen); if(!$.DateTimeMask.DealWith(options.masktype,preText,nCursorPos)) break; strText = preText; nCursorPos++; } if (nCursorPos>strText.length) { nCursorPos=strText.length; } //“eQT,‰9hncS_MRMOn³Qš[/f&T‰ŒteMOn0Ôk‚Y°x0R":" "-" " " if(options.masktype<="3"){ if(nCursorPos==4 || nCursorPos==7 || nCursorPos==10 || nCursorPos==13 || nCursorPos==16) nCursorPos++; } else{ if(nCursorPos==2 || nCursorPos==5 ) nCursorPos++; } break; } objTextBox.value = strText; $.DateTimeMask.Selection(objTextBox,nCursorPos,nCursorPos); }, //9hncIQh@b(W„vMOn ÿ$R­e“eQ„vW[&{/f&TTÕl DealWith : function(masktype,input,nCursorPos) { var ls_date,ls_time; if(masktype <= "3") { ls_year = input.substr(0,4); if(ls_year=="0000") ls_year = "2001"; ls_month = input.substr(5,2); if(ls_month=="00") ls_month = "01"; ls_day = input.substr(8,2); if(ls_day=="00") ls_day = "01"; ls_date = ls_year +"-"+ ls_month +"-"+ ls_day; ls_time = "00:00:00"; if(masktype=="1") { ls_time = input.substr(11); } else { if(masktype=="2") ls_time = input.substr(11) + ":00"; } //IQh@b(W„vMOnۏLˆ$R­eS_MRW[&{2N/f&TTÕl return (nCursorPos<=10?$.DateTimeMask.isValidDate(ls_date):$.DateTimeMask.isValidTime(ls_time)) } else { ls_time = input; if(masktype=="4") ls_time = ls_time + ":00"; //IQh@b(W„vMOnۏLˆ$R­eS_MRW[&{2N/f&TTÕl return $.DateTimeMask.isValidTime(ls_time); } return true; }, //¨R\Oÿ·ƒÖSIQh@b(W„vMOn ÿSìbwËYMOnŒTÓ~_gMOn GetCursor : function(textBox){ var obj = new Object(); var start = 0,end = 0; if ($.browser.mozilla) { start = textBox.selectionStart; end = textBox.selectionEnd; } if ($.browser.msie) { var range=textBox.createTextRange(); var text = range.text; var selrange = document.selection.createRange(); var seltext = selrange.text; while(selrange.compareEndPoints("StartToStart",range)>0){ selrange.moveStart("character",-1); start ++; } while(selrange.compareEndPoints("EndToStart",range)>0){ selrange.moveEnd("character",-1); end ++; } } obj.start = start; obj.end = end; return obj; }, //¨R\Oÿ©‹field„vstart0Rend«ˆ -N Selection : function(field, start, end) { if( field.createTextRange ){ var r = field.createTextRange(); r.moveStart('character',start); r.collapse(true); r.select(); } else if( field.setSelectionRange ){ field.setSelectionRange(start, end); } else { if( field.selectionStart ){ field.selectionStart = start; field.selectionEnd = end; } } field.focus(); }, ///f&T:Nåeg isValidDate : function(strDate) { var ls_regex = "^((((((0[48])|([13579][26])|([2468][048]))00)|([0-9][0-9]((0[48])|([13579][26])|([2468][048]))))-02-29)|(((000[1-9])|(00[1-9][0-9])|(0[1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))-((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30))|(((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-8]))))))$"; var exp = new RegExp(ls_regex, "i"); return exp.test(strDate); }, ///f&T:Nöeô• isValidTime : function(strTime) { var a = strTime.match(/^(\d{2,2})(:)?(\d{2,2})\2(\d{2,2})$/); if (!a || a[1]>23 || a[3]>59 || a[4]>59) return false; return true; }, getDateTime : function(options) { var d = new Date(); var vYear = d.getFullYear(); var vMon = d.getMonth() + 1; vMon = (vMon<10 ? "0" + vMon : vMon); var vDay = d.getDate(); var ls_date = vYear+"-"+vMon+"-"+(vDay<10 ? "0"+ vDay : vDay ); var vHour = d.getHours(); var vMin = d.getMinutes(); var vSec = d.getSeconds(); var ls_time = (vHour<10 ? "0" + vHour : vHour) + ":"+(vMin<10 ? "0" + vMin : vMin)+":"+(vSec<10 ? "0"+ vSec : vSec ); switch(options.masktype) { case "1": return options.isnow?(ls_date + " " + ls_time):(vYear+"-"+vMon+"-"+"01 00:00:00"); case "2": return options.isnow?(ls_date + " " + ls_time.substr(0,5)):(vYear+"-"+vMon+"-"+"01 00:00"); case "3": return options.isnow?ls_date:(vYear+"-"+vMon+"-"+"01"); case "4": return ls_time.substr(0,5); case "5": return ls_time; } } }