如何检查Text中输入的是正确的日期,即数据合法性检查???

零基础学编程by学哥 2001-05-16 03:20:00
加精
if (s1.length()!=8)
{
sr="<font color=red>The input :</font>"+s1+"<font color=red> length must be 8 and yyyymmdd type.</font>";
}
else
{//add code to do check the date or time type is well or not.
}
...全文
130 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lci21 2001-09-28
  • 打赏
  • 举报
回复
sigh
  • 打赏
  • 举报
回复
这个问题,解决的方法很多。
skyyoung 2001-05-17
  • 打赏
  • 举报
回复
楼上洗咩甘麻烦啊,用正则表达式咪得罗。
function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
valid dates with 2 digit month, 2 digit day,
4 digit year. Date separator can be ., -, or /.
Uses combination of regular expressions and
string parsing to validate date.
Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
strValue - String to be tested for validity

RETURNS:
True if valid, otherwise false.

REMARKS:
Avoids some of the limitations of the Date.parse()
method such as the date separator character.
*************************************************/
var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

//check to see if in correct format
if(!objRegExp.test(strValue))
return false; //doesn't match pattern, bad date
else{
var strSeparator = strValue.substring(2,3) //find date separator
var arrayDate = strValue.split(strSeparator); //split date into month, day, year
//create a lookup for months not equal to Feb.
var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
var intDay = parseInt(arrayDate[1]);

//check if month value and day value agree
if(arrayLookup[arrayDate[0]] != null) {
if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
return true; //found in lookup table, good date
}

//check for February
var intYear = parseInt(arrayDate[2]);
var intMonth = parseInt(arrayDate[0]);
if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
return true; //Feb. had valid number of days
}
return false; //any other values, bad date
}
  • 打赏
  • 举报
回复
我找到了一个日期合法性检查的东东,现在我的程序通过了。
<%!
public class checks
{
//检查日期是否有效
public boolean isValidDate (int Year, int Month, int Day) {
if(Year>=1900 && Year<=2050 && Month>=1 && Month<=12 && Day>=1 && Day<=31) {
boolean validDay = true;
switch(Month) {
case 2:
if( isLeapYear(Year) ) {
if(Day>29) validDay = false;
}
else {
if(Day>28) validDay = false;
}
break;
case 4:
if(Day == 31) validDay = false;
break;
case 6:
if(Day == 31) validDay = false;
break;
case 9:
if(Day == 31) validDay = false;
break;
case 11:
if(Day == 31) validDay = false;
}
return validDay;
}
return false;
}

//是否为闰年
public boolean isLeapYear(int year) {
if(year%4 == 0) {
if(year%100 == 0) {
if(year%400 == 0)
return true;
return false;
}
return true;
}
return false;
}

public String checkin(String s1,String t1)
{
String sr="";
if (t1=="1")//char
{
if (s1.length()>80)
{
sr="<font color=red>The input :</font>"+s1+"<font color=red> long more than 80.</font>";
}
}
else
{
if (t1=="2")//num
{
for (int i=0;i<s1.length() ;i++ )
{
char c1=s1.charAt(i);
if ((c1<'0') || (c1>'9'))
{
sr="<font color=red>The input :</font>"+s1+"<font color=red> is not the number.</font>";
break;
}
}
}
else
{
if (t1=="3")//time
{
if (s1.length()!=8)
{
sr="<font color=red>The input :</font>"+s1+"<font color=red> length must be 8 .</font>";
}
else
{//add code to do check the date or time type is well or not.
int iyear,imonth,iday;
iyear=Integer.valueOf(s1.substring(1,4)).intValue();
imonth=Integer.valueOf(s1.substring(5,6)).intValue();
iday=Integer.valueOf(s1.substring(7,8)).intValue();
if (!isValidDate(iyear,imonth,iday))
{
sr="<font color=red>The input :</font>"+s1+"<font color=red> must be yyyymmdd type.</font>";
}
}
}
}
}
return sr;
}
}
%>
BrentIvan 2001-05-16
  • 打赏
  • 举报
回复
好象没有,用try() catch吧
  • 打赏
  • 举报
回复
有没有JSP的?
BrentIvan 2001-05-16
  • 打赏
  • 举报
回复
用VbScript的IsDate检查吧,Js可以调用Vb的
<script language="VbScript">
Function ValidDate(str)
ValidDate = IsDate(str)
End Function
</script>
<script language="JavaScript">
var str1 = "2001-02-28", str2 = "2001-02-29";
alert(str1 + " is valid date? " + ValidDate(str1));
alert(str2 + " is valid date? " + ValidDate(str2));
</script>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧