Javascript有效日期检查在IE8(和Firefox)中不起作用 [英] Javascript Valid Date Checking does not work in IE8 (and Firefox)
问题描述
我在。
感谢@mplungjan。我赞成了这个答案。
@mplungjan方法(首次提出)列在 http://jsfiddle.net/Lijo/uzSU6/7/ 。 IE8在一个场景中失败了 - http://jsfiddle.net/Lijo/uzSU6/12/ 。
所以我在引用如何验证日期?。请在此处查看 http://jsfiddle.net/Lijo/uzSU6/20/
编辑
请参阅 http://jsfiddle.net/uzSU6/37/ 处理空格的方案
请随意用这种方法提出建议/挑战。
参考文献
- 检查是否存在空格而不使用trim
- 在JavaScript比较中应该使用哪个等于运算符(== vs ===)?
- 如何验证日期?
代码
function isValidDate(s)
{
var bits = s.split('/');
if(s.indexOf('')!= -1)
{
//原始日期字符串中存在空格
返回false;
}
// Javascript月份从零开始
var d = new Date(bits [2],bits [0] - 1,bits [1]);
if(isNaN(Number(bits [2])))
{
//年份无效数字
返回false;
}
if(Number(bits [2])< 1)
{
//年份应大于零
返回false;
}
// 1。检查年份是否为
// 2。检查日期部分是否与原始日期组件相同
// 3。检查d是否有效
return d&& ((d.getMonth()+ 1)== bits [0])&& (d.getDate()== Number(bits [1]));
}
I have tried two popular answers from Detecting an "invalid date" Date instance in JavaScript for checking valid dates. I tested both of them in IE8 – Unfortunately both are disappointing. See it here http://jsfiddle.net/Lijo/uzSU6/2/
Is there a better JavaScript code that will work in IE8 + Chrome + Firefox?
Note: To my surprise, it doesn't work well in Firefox too...
CONDITION
The date format is expected to be US date format with slashes (/)
CODE
isValidDateCheck2('12/33/2012') ;
isValidDateCheck1('12/12/2012') ;
function isValidDateCheck1(d)
{
alert(Object.prototype.toString.call(d));
if ( Object.prototype.toString.call(d) !== "[object Date]" )
{
alert('Not Valid');
}
if(!isNaN(d.getTime()))
{
alert(d.getTime());
}
}
function isValidDateCheck2(d)
{
var timestamp=Date.parse(d);
alert(timestamp);
if (isNaN(timestamp)==false)
{
var date=new Date(timestamp);
alert(date);
}
}
EDIT
@mplungjan approach (first suggested) is listed in http://jsfiddle.net/Lijo/uzSU6/7/. This was failed in IE8 for one scenario - http://jsfiddle.net/Lijo/uzSU6/12/.
Thanks to @mplungjan. I have upvoted that answer.
@mplungjan approach (first suggested) is listed in http://jsfiddle.net/Lijo/uzSU6/7/. This was failed in IE8 for one scenario - http://jsfiddle.net/Lijo/uzSU6/12/.
So I have used a slightly different approach after referring How to validate a date?. See it here http://jsfiddle.net/Lijo/uzSU6/20/
EDIT
Please refer http://jsfiddle.net/uzSU6/37/ for scenarios that handle blank spaces
Feel free to give your suggestions/ challenges with this approach.
References
- Check whether white spaces exist without using trim
- Which equals operator (== vs ===) should be used in JavaScript comparisons?
- How to validate a date?
CODE
function isValidDate(s)
{
var bits = s.split('/');
if(s.indexOf(' ') != -1)
{
//White space exists in the original date string
return false;
}
//Javascript month starts at zero
var d = new Date(bits[2], bits[0] - 1, bits[1]);
if ( isNaN( Number(bits[2]) ) )
{
//Year is not valid number
return false;
}
if ( Number(bits[2]) < 1 )
{
//Year should be greater than zero
return false;
}
//1. Check whether the year is a Number
//2. Check whether the date parts are eqaul to original date components
//3. Check whether d is valid
return d && ( (d.getMonth() + 1) == bits[0]) && (d.getDate() == Number(bits[1]) );
}
这篇关于Javascript有效日期检查在IE8(和Firefox)中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!