<table width="266" cellpadding="1" cellspacing="0">
<tr>
<td width="15"><img src="image/p_2.gif" alt=";;" width="15" height="11"></td>
<td width="116"><input name="Date_1" type="text" id="da_1" onblur="strDateTime(this.value)" value="<%= newdate_2 %>" size="10">
<input name="button2" type=button onClick="return showCalendar('Date_1', 'y-mm-dd');" value=".." ></td>
<td width="7">至</td>
<td width="118"><INPUT id="da_2" size="10" name="Date_2" onblur="strDateTime(this.value)" value="<%= newdate %>">
<INPUT name="button" type=button onclick="return showCalendar('Date_2', 'y-mm-dd');" value=".."></td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE4"><font size="2" color="#660000">选择时间段</font></span></span></td>
<td> </td>
<td><input type="submit" name="Submit2" value="开始查询"></td>
</tr>
</table>
<script language=javascript>
function strDateTime(str)
{
var reg =/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; //这个正则,输入的是YYYY-MM-DD时就报格式不对
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
if (newStr != str) alert("时间格式不对!");
}
</script>
求高手修改,当输入的是YYYY-MM-DD不报错,其它格式的报错
或者YYYY-MM-DD(必须的),YYYYMMDD不报错,其它格式的报错
9 个解决方案
#1
<script>
//判断输入的日期是否正确
function CheckDate(INDate)
{
if(INDate=="")
{return true;}
subYY=INDate.substr(0,4)
if(isNaN(subYY)||subYY<=0)
{return true;}
//转换年份
if(INDate.indexOf('-',0)!=-1)
{separate="-"}
else{if(INDate.indexOf('/',0)!=-1)
{separate="/"}
else {return true;}
}
area=INDate.indexOf(separate,0)
subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))
if(isNaN(subMM)||subMM<=0)
{return true;}
if(subMM.length<2){subMM="0"+subMM}
//转换日
area=INDate.lastIndexOf(separate)
subDD=INDate.substr(area+1,INDate.length-area-1)
if(isNaN(subDD)||subDD<=0)
{return true;}
if(eval(subDD)<10){subDD="0"+eval(subDD)}
NewDate=subYY+"-"+subMM+"-"+subDD
if(NewDate.length!=10){return true;}
if(NewDate.substr(4,1)!="-"){return true;}
if(NewDate.substr(7,1)!="-"){return true;}
var MM=NewDate.substr(5,2);
var DD=NewDate.substr(8,2);
if((subYY%4==0&&subYY%100!=0)||subYY%400==0){//判断是否为闰年
if(parseInt(MM)==2){
if(DD>29){return true;}
}
}else{
if(parseInt(MM)==2){if(DD>28){return true;}}
}
var mm=new Array(1,3,5,7,8,10,12);//判断每月中的最大天数
for(i=0;i<mm.length;i++)
{
if(parseInt(MM)==mm[i])
{
if(parseInt(DD)>31){return true;}
}
}
var mm1=new Array(2,4,6,9,11);
for(i=0;i<mm1.length;i++)
{
if(parseInt(MM)==mm1[i])
{
if(parseInt(DD)>30){return true;}
}
}
if(parseInt(MM)>12){return true;}
return false;
}
</script>
#2
code=VBScript]
<table width="266" cellpadding="1" cellspacing="0">
<tr>
<td width="15"><img src="image/p_2.gif" alt=";;" width="15" height="11"></td>
<td width="116"><input name="Date_1" type="text" id="da_1" onblur="strDateTime(this.value)" value="<%= newdate_2 %>" size="10">
<input name="button2" type=button onClick="return showCalendar('Date_1', 'y-mm-dd');" value=".." ></td>
<td width="7">至</td>
<td width="118"><INPUT id="da_2" size="10" name="Date_2" onblur="strDateTime(this.value)" value="<%= newdate %>">
<INPUT name="button" type=button onclick="return showCalendar('Date_2', 'y-mm-dd');" value=".."></td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE4"><font size="2" color="#660000">选择时间段</font></span></span></td>
<td> </td>
<td><input type="submit" name="Submit2" value="开始查询"></td>
</tr>
</table>
[/code]
<table width="266" cellpadding="1" cellspacing="0">
<tr>
<td width="15"><img src="image/p_2.gif" alt=";;" width="15" height="11"></td>
<td width="116"><input name="Date_1" type="text" id="da_1" onblur="strDateTime(this.value)" value="<%= newdate_2 %>" size="10">
<input name="button2" type=button onClick="return showCalendar('Date_1', 'y-mm-dd');" value=".." ></td>
<td width="7">至</td>
<td width="118"><INPUT id="da_2" size="10" name="Date_2" onblur="strDateTime(this.value)" value="<%= newdate %>">
<INPUT name="button" type=button onclick="return showCalendar('Date_2', 'y-mm-dd');" value=".."></td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE4"><font size="2" color="#660000">选择时间段</font></span></span></td>
<td> </td>
<td><input type="submit" name="Submit2" value="开始查询"></td>
</tr>
</table>
[/code]
[
<script language=javascript>
function strDateTime(str)
{
var reg =/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; //这个正则,输入的是YYYY-MM-DD时就报格式不对
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
if (newStr != str) alert("时间格式不对!");
}
</script>
#3
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
</script>
#4
你这个怎么用?输入框如何用
#5
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('0000-00-00');
</script>
试过这个没?
^_^
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('0000-00-00');
</script>
试过这个没?
^_^
#6
调用ValidDate()函数啊。
<input type=text name=a id=a onpropertychange='javascript:return ValidDate(this.value)'>
或者你把onpropertychange换成其他事件
#7
算了,写完整吧:))
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
</script>
<input type=text name=a id=a onpropertychange='javascript:return ValidDate(this.value)'> IE
<input type=text name=a id=a oninput='javascript:return ValidDate(this.value)'> FF
#8
把7楼的
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
删除
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
删除
#9
<script language="javascript">
function isDate(dateStr)
{
if(dateStr.replace(/\d/g,"")=="" && dateStr.length==8)
dateStr = dateStr.substr(0,4)+"-"+dateStr.substr(4,2)+"-"+dateStr.substr(6,2);
var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("格式是YYYY-MM-DD 或者 YYYYMMDD.");
return false;
}
year = matchArray[1]; // parse date into variables
month = matchArray[3];
day = matchArray[5];
if (month < 1 || month > 12) { // check month range
alert("月份的值是 1 到 12.");
return false;
}
if (day < 1 || day > 31) {
alert("天数是在1和31之间.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("月份 "+month+" 没有31天!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert(year+"年的二月 没有 " + day + " 天!");
return false;
}
}
return true; // date is valid
}
alert(isDate("2002-02-29"));
alert(isDate("20030229"));
alert(isDate("2002-12-29"));
alert(isDate("20031229"));
</script>
#1
<script>
//判断输入的日期是否正确
function CheckDate(INDate)
{
if(INDate=="")
{return true;}
subYY=INDate.substr(0,4)
if(isNaN(subYY)||subYY<=0)
{return true;}
//转换年份
if(INDate.indexOf('-',0)!=-1)
{separate="-"}
else{if(INDate.indexOf('/',0)!=-1)
{separate="/"}
else {return true;}
}
area=INDate.indexOf(separate,0)
subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))
if(isNaN(subMM)||subMM<=0)
{return true;}
if(subMM.length<2){subMM="0"+subMM}
//转换日
area=INDate.lastIndexOf(separate)
subDD=INDate.substr(area+1,INDate.length-area-1)
if(isNaN(subDD)||subDD<=0)
{return true;}
if(eval(subDD)<10){subDD="0"+eval(subDD)}
NewDate=subYY+"-"+subMM+"-"+subDD
if(NewDate.length!=10){return true;}
if(NewDate.substr(4,1)!="-"){return true;}
if(NewDate.substr(7,1)!="-"){return true;}
var MM=NewDate.substr(5,2);
var DD=NewDate.substr(8,2);
if((subYY%4==0&&subYY%100!=0)||subYY%400==0){//判断是否为闰年
if(parseInt(MM)==2){
if(DD>29){return true;}
}
}else{
if(parseInt(MM)==2){if(DD>28){return true;}}
}
var mm=new Array(1,3,5,7,8,10,12);//判断每月中的最大天数
for(i=0;i<mm.length;i++)
{
if(parseInt(MM)==mm[i])
{
if(parseInt(DD)>31){return true;}
}
}
var mm1=new Array(2,4,6,9,11);
for(i=0;i<mm1.length;i++)
{
if(parseInt(MM)==mm1[i])
{
if(parseInt(DD)>30){return true;}
}
}
if(parseInt(MM)>12){return true;}
return false;
}
</script>
#2
code=VBScript]
<table width="266" cellpadding="1" cellspacing="0">
<tr>
<td width="15"><img src="image/p_2.gif" alt=";;" width="15" height="11"></td>
<td width="116"><input name="Date_1" type="text" id="da_1" onblur="strDateTime(this.value)" value="<%= newdate_2 %>" size="10">
<input name="button2" type=button onClick="return showCalendar('Date_1', 'y-mm-dd');" value=".." ></td>
<td width="7">至</td>
<td width="118"><INPUT id="da_2" size="10" name="Date_2" onblur="strDateTime(this.value)" value="<%= newdate %>">
<INPUT name="button" type=button onclick="return showCalendar('Date_2', 'y-mm-dd');" value=".."></td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE4"><font size="2" color="#660000">选择时间段</font></span></span></td>
<td> </td>
<td><input type="submit" name="Submit2" value="开始查询"></td>
</tr>
</table>
[/code]
<table width="266" cellpadding="1" cellspacing="0">
<tr>
<td width="15"><img src="image/p_2.gif" alt=";;" width="15" height="11"></td>
<td width="116"><input name="Date_1" type="text" id="da_1" onblur="strDateTime(this.value)" value="<%= newdate_2 %>" size="10">
<input name="button2" type=button onClick="return showCalendar('Date_1', 'y-mm-dd');" value=".." ></td>
<td width="7">至</td>
<td width="118"><INPUT id="da_2" size="10" name="Date_2" onblur="strDateTime(this.value)" value="<%= newdate %>">
<INPUT name="button" type=button onclick="return showCalendar('Date_2', 'y-mm-dd');" value=".."></td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE4"><font size="2" color="#660000">选择时间段</font></span></span></td>
<td> </td>
<td><input type="submit" name="Submit2" value="开始查询"></td>
</tr>
</table>
[/code]
[
<script language=javascript>
function strDateTime(str)
{
var reg =/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; //这个正则,输入的是YYYY-MM-DD时就报格式不对
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
if (newStr != str) alert("时间格式不对!");
}
</script>
#3
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
</script>
#4
你这个怎么用?输入框如何用
#5
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('0000-00-00');
</script>
试过这个没?
^_^
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('0000-00-00');
</script>
试过这个没?
^_^
#6
调用ValidDate()函数啊。
<input type=text name=a id=a onpropertychange='javascript:return ValidDate(this.value)'>
或者你把onpropertychange换成其他事件
#7
算了,写完整吧:))
<script>
function ValidDate(s){
var pattern=/^(\d{4})-(\d{1,})-(\d{1,})$/i
var matches=s.match(pattern);
if(matches){ //匹配 yyyy-mm-dd 的形式
var tempDate=new Date(matches[1],matches[2]-1,matches[3]);
if(tempDate.getYear()!=matches[1]||tempDate.getMonth()!=(matches[2]-1)||tempDate.getDate()!=matches[3]){//检查是否正确日期,比如9999-99-99等形式
alert("日期不正确!");
return false;
}
}
else{
alert("格式错误");
return false;
}
return true;
}
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
</script>
<input type=text name=a id=a onpropertychange='javascript:return ValidDate(this.value)'> IE
<input type=text name=a id=a oninput='javascript:return ValidDate(this.value)'> FF
#8
把7楼的
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
删除
ValidDate('200-9-1');
ValidDate('9999-89-01');
ValidDate('2008-12-01');
删除
#9
<script language="javascript">
function isDate(dateStr)
{
if(dateStr.replace(/\d/g,"")=="" && dateStr.length==8)
dateStr = dateStr.substr(0,4)+"-"+dateStr.substr(4,2)+"-"+dateStr.substr(6,2);
var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("格式是YYYY-MM-DD 或者 YYYYMMDD.");
return false;
}
year = matchArray[1]; // parse date into variables
month = matchArray[3];
day = matchArray[5];
if (month < 1 || month > 12) { // check month range
alert("月份的值是 1 到 12.");
return false;
}
if (day < 1 || day > 31) {
alert("天数是在1和31之间.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("月份 "+month+" 没有31天!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert(year+"年的二月 没有 " + day + " 天!");
return false;
}
}
return true; // date is valid
}
alert(isDate("2002-02-29"));
alert(isDate("20030229"));
alert(isDate("2002-12-29"));
alert(isDate("20031229"));
</script>