日期:2014-05-16  浏览次数:20365 次

【转】JS 时间日期控件
<html>
<body>

  <script>
function isTime(str){
   var a = str.match(/^(\d{0,2}):(\d{0,2}):(\d{0,2})$/);
   if (a == null) return false;
   if (a[1]>=24 || a[2]>=60 || a[3]>=60) return false;
   return true;
}
function isDateTime(str){
   var a = str.match(/^(\d{0,4})-(\d{0,2})-(\d{0,2}) (\d{0,2}):(\d{0,2}):(\d{0,2})$/);
   if (a == null) return false;
   if ( a[2]>=13 || a[3]>=32 || a[4]>=24 || a[5]>=60 || a[6]>=60) return false;
   return true;
}
function isDate(str){
   var a = str.match(/^(\d{0,4})-(\d{0,2})-(\d{0,2})$/);
   if (a == null) return false;
   if ( a[2]>=13 || a[3]>=32 || a[4]>=24) return false;
   return true;
}
function validate(obj,type){
var range=obj.createTextRange(); 
var text = range.text;
var selrange = document.selection.createRange();
var seltext = selrange.text;
var startpos = 0,endpos = 0;
while(selrange.compareEndPoints("StartToStart",range)>0){ 
   selrange.moveStart("character",-1);  
   startpos ++;
}
while(selrange.compareEndPoints("EndToStart",range)>0){ 
   selrange.moveEnd("character",-1);  
   endpos ++;
}
if(event.keyCode>=48){ 
   var keytext = String.fromCharCode(event.keyCode);
   text = text.substring(0,startpos) + keytext + text.substring(endpos,text.length);
}else if(event.keyCode == 46){//delete
   if(startpos == endpos)text = text.substring(0,startpos) + text.substring(startpos+1,text.length);
   else text = text.substring(0,startpos) + text.substring(endpos,text.length);
}else if(event.keyCode == 8){
   if(startpos == endpos)text = text.substring(0,startpos-1) + text.substring(startpos,text.length);
   else text = text.substring(0,startpos) + text.substring(endpos,text.length);
}
if(event.keyCode == 45){
   event.returnValue = false;
   return;
}
var valid;
switch(type){
   case 1:valid = isDate(text);break;
   case 2:valid = isTime(text);break;
   case 3:valid = isDateTime(text);break;
   default:valid = false;
}
if(!valid){
event.returnValue = false;
}
}
</script>
<script>
function PopupCalendar(InstanceName)
{
///Global Tag
this.instanceName=InstanceName;
///Properties
this.separator="-"
this.oBtnTodayTitle="Today"
this.oBtnCancelTitle="Cancel"
this.weekDaySting=new Array("S","M","T","W","T","F","S");
this.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
this.Width=200;
this.currDate=new Date();
this.today=new Date();
this.startYear=1950;
this.endYear=2010;
///Css
this.divBorderCss="1px solid #BCD0DE";
this.tableBorderColor="#CCCCCC"
///Method
this.Init=CalendarInit;
this.Fill=CalendarFill;
this.Refresh=CalendarRefresh;
this.Restore=CalendarRestore;
///HTMLObject
this.oTaget=null;
this.oPreviousCell=null;
this.sDIVID=InstanceName+"oDiv";
this.sTABLEID=InstanceName+"oTable";
this.sMONTHID=InstanceName+"oMonth";
this.sYEARID=InstanceName+"oYear";

}
function CalendarInit()     ///Create panel
{
var sMonth,sYear
sMonth=this.currDate.getMonth();
sYear=this.currDate.getYear();
htmlAll="<div id='"+this.sDIVID+"' style='display:none;position:absolute;width:"+this.Width+";border:"+this.divBorderCss+";padding:2px;background-color:#FFFFFF'>";
htmlAll+="<div align='center'>";
/// Month
htmloMonth="<select id='"+this.sMONTHID+"' onchange=CalendarMonthChange("+this.instanceName+") style='width:50%'>";
for(i=0;i<12;i++)
{   
   htmloMonth+="<option value='"+i+"'>"+this.monthSting[i]+"</option>";
}
htmloMonth+="</select>";
/// Year
htmloYear="<select id='"+this.sYEARID+"' onchange=CalendarYearChange("+this.instanceName+") style='width:50%'>";
for(i=this.startYear;i<=this.endYear;i++)
{
   htmloYear+="<option value='"+i+"'>"+i+"</option>";
}
htmloYear+="</select></div>";
/// Day
htmloDayTable="<table id='"+this.sTABLEID+"' width='100%' border=0 cellpadding=0 cellspacing=1 bgcolor='"+this.tableBorderColor+"'>";