|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622 。
您需要 登录 才可以下载或查看,没有帐号?注册
x
begindate 和enddate 的之间 校验 即enddate需要大于等于bigindate
以及enddate与当前日期的比较 即enddate需要小于当前日期
完全没学过javascript 不知道怎么下手,求指导
<script language="javascript">
function isDate( s ){
var len = s.length
var year, month, day
var i, j, k
if ( len < 8 ) return false
i = 0
while ( i < len ) {
if ( s.charAt(i) == ' ' ) {
i++
} else {
break
}
}
year = 0
j = 0
while ( i < len ) {
if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
//alert (s.charAt(i))
j++
if ( j > 4 ) return false
year = year * 10 + parseInt( "" + s.charAt(i))
i++
} else {
if ( s.charAt(i) == '-' ) { break }
return false
}
}
if ( year < 1990 || year > 2050 ) { return false }
i++
if ( i >= len ) { return false }
month = 0
j = 0
while ( i < len ) {
if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return false
month = month * 10 + parseInt(s.charAt(i))
i++
} else {
if ( s.charAt(i) == '-' ) { break }
return false
}
}
if ( month <= 0 || month > 12 ) { return false }
i++
if ( i >= len ) { return false }
day = 0
j = 0
while ( i < len ) {
if ( s.charAt(i) >= '0' && s.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return false
day = day * 10 + parseInt(s.charAt(i))
i++
} else {
if ( s.charAt(i) == ' ' ) { break }
return false}
}
if ( day <= 0 || day > 32 ) { return false }
while ( i < len ) {
if ( s.charAt(i) == ' ' ) {
i++} else {
break}}
if ( i < len ) { return false }
switch (month) {
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
if ( day > 31 ) { return false }
break
case 4 : case 6 : case 9 : case 11 :
if ( day > 30 ) { return false }
break
case 2 :
if ( (year % 4) != 0 || ( (year % 100) == 0 && (year % 400) != 0 ) ) {
if ( day > 28 ) { return false }
} else {
if ( day > 29 ) { return false }
}
break
default :
return false
break}
if (day == 0) {return false }
return true
}
function DateCompare( s1, s2 ){
var len = s1.length
var year1, month1, day1
var year2, month2, day2
var i, j, k
i = 0
while ( i < len ) {
if ( s1.charAt(i) == ' ' ) {
i++
} else {
break}
}
year1 = 0
j = 0
while ( i < len ) {
if ( s1.charAt(i) >= '0' && s1.charAt(i) <= '9' ) {
j++
if ( j > 4 ) return -1
year1 = year1 * 10 + parseInt( "" + s1.charAt(i))
i++} else {
if ( s1.charAt(i) == '-' ) { break }
return -1 }}
if ( year1 < 1990 && year1 > 2050 ) { return -1 }
i++
if ( i >= len ) { return -1 }
month1 = 0
j = 0
while ( i < len ) {
if ( s1.charAt(i) >= '0' && s1.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return -1
month1 = month1 * 10 + parseInt( "" + s1.charAt(i))
i++
} else {
if ( s1.charAt(i) == '-' ) { break }
return -1 }}
if ( month1 < 0 && month1 > 12 ) { return -1 }
i++
if ( i >= len ) { return -1 }
day1 = 0
j = 0
while ( i < len ) {
if ( s1.charAt(i) >= '0' && s1.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return -1
day1 = day1 * 10 + parseInt( "" + s1.charAt(i))
i++
} else {
if ( s1.charAt(i) == ' ' ) { break }
return -1 }}
if ( day1 < 0 && day1 > 32 ) { return -1 }
while ( i < len ) {
if ( s1.charAt(i) == ' ' ) {
i++
} else {
break}}
if ( i < len ) { return -1 }
switch (month1) {
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
if ( day1 > 31 ) { return -1 }
break
case 4 : case 6 : case 9 : case 11 :
if ( day1 > 30 ) { return -1 }
break
case 2 :
if ( (year1 % 4) != 0 || ( (year1 % 100) == 0 && (year1 % 400) != 0 ) ) {
if ( day1 > 28 ) { return -1 }
} else {
if ( day1 > 29 ) { return -1 } }
break
default :
return -1
break}
//S2
len = s2.length
i = 0
while ( i < len ) {
if ( s2.charAt(i) == ' ' ) {
i++
} else {
break }}
year2 = 0
j = 0
while ( i < len ) {
if ( s2.charAt(i) >= '0' && s2.charAt(i) <= '9' ) {
j++
if ( j > 4 ) return -2
year2 = year2 * 10 + parseInt( "" + s2.charAt(i))
i++
} else {
if ( s2.charAt(i) == '-' ) { break }
return -2 }}
if ( year2 < 1990 && year2 > 2050 ) { return -2 }
i++
if ( i >= len ) { return -2 }
month2 = 0
j = 0
while ( i < len ) {
if ( s2.charAt(i) >= '0' && s2.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return -2
month2 = month2 * 10 + parseInt( "" + s2.charAt(i))
i++
} else {
if ( s2.charAt(i) == '-' ) { break }
return -2 }}
if ( month2 < 0 && month2 > 12 ) { return -2 }
i++
if ( i >= len ) { return -2 }
day2 = 0
j = 0
while ( i < len ) {
if ( s2.charAt(i) >= '0' && s2.charAt(i) <= '9' ) {
j++
if ( j > 2 ) return -2
day2 = day2 * 10 + parseInt( "" + s2.charAt(i))
i++
} else {
if ( s2.charAt(i) == ' ' ) { break }
return -2 }}
if ( day2 < 0 && day2 > 32 ) { return -2 }
while ( i < len ) {
if ( s2.charAt(i) == ' ' ) {
i++
} else {
break}}
if ( i < len ) { return -2 }
switch (month2) {
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
if ( day2 > 31 ) { return -2 }
break
case 4 : case 6 : case 9 : case 11 :
if ( day2 > 30 ) { return -2 }
break
case 2 :
if ( (year2 % 4) != 0 || ( (year2 % 100) == 0 && (year2 % 400) != 0 ) ) {
if ( day2 > 28 ) { return -2 }
} else {
if ( day2 > 29 ) { return -2 } }
break
default :
return -2
break}
//比较
if ( year1 > year2 ) { return 3 }
if ( year1 < year2 ) { return 1 }
if ( month1 > month2 ) { return 3 }
if ( month1 < month2 ) { return 1 }
if ( day1 > day2 ) { return 3 }
if ( day1 < day2 ) { return 1 }
return 2
//-1:第一个日期有误, -2:第二日期有误, 1:S2大于S1, 2:相等, 3:S1大于S2
}
function p_inputdate(objectID){
var getValue = document.getElementById(objectID).value;
if(CheckPass==true){
if( isDate( getValue ) == false ){
CheckPass = false ;
alert("输入有误,应符合:\n\n" + "日期格式 YYYY-MM-DD 或 YYYY-M-D,且日期有效");
document.getElementById(objectID).select();
return ;}}}
function p_StartDateEndDate(objectID_StartDate,objectID_EndDate ){
var StartDate = document.getElementById(objectID_StartDate).value;
var EndDate = document.getElementById(objectID_EndDate).value;
var DateCompareReturn;
p_inputdate(objectID_StartDate);
p_inputdate(objectID_EndDate);
if(CheckPass==true){
DateCompareReturn = DateCompare( StartDate, EndDate );
if( DateCompareReturn == -1 ){
CheckPass = false ;
alert("输入有误,应符合:\n\n" + "日期格式 YYYY-MM-DD 或 YYYY-M-D,且日期有效");
document.getElementById(objectID_StartDate).select();
return ;}
if( DateCompareReturn == -2 ){
CheckPass = false ;
alert("输入有误,应符合:\n\n" + "日期格式 YYYY-MM-DD 或 YYYY-M-D,且日期有效");
document.getElementById(objectID_EndDate).select();
return ;}
if( DateCompareReturn == 3 ){
CheckPass = false ;
alert("输入有误,应符合:\n\n" + "开始日期小于等于结束日期");
document.getElementById(objectID_EndDate).select();
return ;}}}
function checkform(){
CheckPass = true;
ReturnVal = 0;
p_StartDateEndDate("txtDatestart_date", "txtDateend_date");
if(CheckPass == true ) {
promptButtonFinish(); } }
</script> |
|
|
|
|