验证日期格式

C# 验证日期格式: 

  protected void VerifyApplyRevDateFormat(object source, ServerValidateEventArgs args) { try { if (string.IsNullOrEmpty(args.Value)) { args.IsValid = true; } else { string format = "dd/MM/yyyy HH:mm:ss"; if ((args.Value.Length <= format.Length || args.Value.Length >= format.Length - 5) && (args.Value.IndexOf('/') == 2 || args.Value.IndexOf('/') == 1) && (args.Value.IndexOf('/') != args.Value.LastIndexOf('/'))) { string dayStr = args.Value.Substring(0, args.Value.IndexOf('/')); string monthStr = args.Value.Substring(args.Value.IndexOf('/') + 1, args.Value.LastIndexOf('/') - args.Value.IndexOf('/') - 1); string dateStr = monthStr + "/" + dayStr + args.Value.Substring(args.Value.LastIndexOf('/')); DateTime receiveDate = DateTime.Parse(dateStr); args.IsValid = true; } else { args.IsValid = false; } } } catch { args.IsValid = false; } }

 

JavaScript 验证日期格式:

function CheckDate(){ var strDate = document.getElementById("<%=tbxDate.ClientID %>").value; strDate=strDate.replace(/(^/s*)|(/s*$)/g,""); // var reg=/^(/d{2})//(/d{2})//(/d{4})$/; //date format: dd/MM/yyyy // var reg=/(^/s*)(/d{1}|/d{2})//(/d{1}|/d{2})//(/d{4}) ((0?[0-9])|([1][0-9])|([2][0-3]))/:((0?[0-9])|([1-5][0-9]))/:((0?[0-9])|([1-5][0-9]))(/s*$)/; //date format: dd/MM/yyyy hh:mm:ss var reg=/(^/s*)(/d{1}|/d{2})(/s*)//(/s*)(/d{1}|/d{2})(/s*)//(/s*)(/d{4})(/s*) ((0?[0-9])|([1][0-9])|([2][0-3]))(/s*)/:(/s*)((0?[0-9])|([1-5][0-9]))(/s*)/:(/s*)((0?[0-9])|([1-5][0-9]))(/s*$)/; //date format:dd/MM/yyyy hh:mm:ss, 允许空格在'/',':'前后 if(!reg.test(strDate)){ alert("日期格式不正确!/n正确格式为:20040101"); return false; } //var ss=strDate.split("/"); //var year=ss[0]; //var month=ss[1]; //var date=ss[2]; var year=strDate.substr(strDate.lastIndexOf('/')+1).replace(/(^/s*)|(/s*$)/g,"").substr(0,4); var month=strDate.substr(strDate.indexOf('/')+1).replace(/(^/s*)|(/s*$)/g,"").substring(0,strDate.substr(strDate.indexOf('/')+1).replace(/(^/s*)|(/s*$)/g,"").lastIndexOf('/')).replace(/(^/s*)|(/s*$)/g,""); var date=strDate.substring(0,strDate.indexOf('/')).replace(/(^/s*)|(/s*$)/g,""); alert(year+'-'+month+'-'+date); if(!checkYear(year)){return false;} if(!checkMonth(month)){return false;} if(!checkDate(year,month,date)){return false;} alert("right"); var time=strDate.substr(strDate.lastIndexOf('/')+1).replace(/(^/s*)|(/s*$)/g,"").substr(4).replace(/(^/s*)|(/s*$)/g,""); // var hour=strDate.substring(strDate.indexOf(' ')+1,strDate.indexOf(':')); // var minute=strDate.substring(strDate.indexOf(':')+1,strDate.lastIndexOf(':')); // var second=strDate.substr(strDate.lastIndexOf(':')+1); var hour=time.substring(0,time.indexOf(':')).replace(/(^/s*)|(/s*$)/g,""); var minute=time.substr(time.indexOf(':')+1).replace(/(^/s*)|(/s*$)/g,"").substring(0,time.substr(time.indexOf(':')+1).replace(/(^/s*)|(/s*$)/g,"").lastIndexOf(':')).replace(/(^/s*)|(/s*$)/g,""); var second=time.substr(time.lastIndexOf(':')+1).replace(/(^/s*)|(/s*$)/g,""); alert(hour+':'+minute+':'+second); document.getElementById("<%=tbxDate.ClientID %>").value=getStandardParam(date)+'/'+getStandardParam(month)+'/'+year+' '+getStandardParam(hour)+':'+getStandardParam(minute)+':'+getStandardParam(second); return true; } function getStandardParam(originalStr) { if(originalStr.toString().length==1) { var result='0'+originalStr; return result; } else return originalStr; } function checkYear(year){ if(isNaN(parseInt(year,10))){ alert("年份输入有误,请重新输入!"); return false; } else if(parseInt(year,10)<1900 || parseInt(year,10) >2099) { alert("年份应该在1950-2050之间!"); return false; } else return true; } function checkMonth(month){ if(isNaN(parseInt(month,10))){alert("月份输入有误,请重新输入!"); return false;} else if(parseInt(month,10)<1 || parseInt(month,10) >12) { alert("月份应该在1-12之间!"); return false; } else return true; } function checkDate(year,month,date){ var daysOfMonth=CalDays(parseInt(year,10),parseInt(month,10)); if(isNaN(parseInt(date,10))){alert("日期输入有误,请重新输入!"); return false;} else if(parseInt(date,10)<1||parseInt(date,10)>daysOfMonth){ alert("日期应该在1-"+daysOfMonth+"之间!"); return false;} else{return true;} } function CalDays(year,month){ var date= new Date(year,month,0); return date.getDate(); } function isLeapYear(year){ if((year %4==0 && year %100!=0) || (year %400==0)) return true; else return false; }   

 

 

Javascript的验证日期控件是在前人的基础上修改完成的。

 

你可能感兴趣的:(JavaScript,Date,String,function,object,C#)