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.
}
...全文
1307打赏收藏
如何检查Text中输入的是正确的日期,即数据合法性检查???
if (s1.length()!=8) { sr="The input :"+s1+" length must be 8 and yyyymmdd type."; } else {//add code to do check the date or time type is well or not. }
楼上洗咩甘麻烦啊,用正则表达式咪得罗。
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 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;
}
}
%>