当前位置: 技术问答>java相关
如何检查Text中输入的是正确的日期,即数据合法性检查???
来源: 互联网 发布时间:2015-02-07
本文导语: 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. } | 用VbScript的IsDate检查吧,Js可以调用Vb的 Function ValidDate(str) Va...
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.
}
{
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.
}
|
用VbScript的IsDate检查吧,Js可以调用Vb的
Function ValidDate(str)
ValidDate = IsDate(str)
End Function
var str1 = "2001-02-28", str2 = "2001-02-29";
alert(str1 + " is valid date? " + ValidDate(str1));
alert(str2 + " is valid date? " + ValidDate(str2));
Function ValidDate(str)
ValidDate = IsDate(str)
End Function
var str1 = "2001-02-28", str2 = "2001-02-29";
alert(str1 + " is valid date? " + ValidDate(str1));
alert(str2 + " is valid date? " + ValidDate(str2));
|
楼上洗咩甘麻烦啊,用正则表达式咪得罗。
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}1d{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
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}1d{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