繁体   English   中英

使用正则表达式验证日期格式

[英]Validate the date format withou using regular expression

我正在尝试编写不使用正则表达式的javascript日期格式验证。 当我输入日期26/02/2013时,我的警报值为2093年3月2日星期三,并且错误消息该日期不是有效格式

任何帮助将不胜感激

    function CheckDateFormat(StartDateform)     

    {   
            var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;        
            spl = StartDateform.split("/"); 


            var testDate=new Date(spl[2],spl[0]-1,spl[1]);              

            year= testDate.getFullYear();                   
            month = testDate.getMonth()+1;                  
            day= testDate.getDate();

            alert ("the date value is "+ " "+ testDate);

        if (day!==spl[1] || month!==spl[0] || year!==spl[2])                    
        {
            alert ("the date value is "+ " "+ testDate);
            alert(StartDateform + " " + "Is an Invalid Date. Please use the format 'MM/DD/YYYY'");
            return false;
        }


        return true;                        
    }                   

    function Submit() 
    {
        if(CheckDateFormat())
        {
        $('button_submit').click(); 
        alert('New rate submitted');
        }
    }

Date构造函数期望Y / M / D:

Date -创建JavaScript日期实例,使您可以使用日期和时间。

句法

new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day [hour, minute, second, millisecond]);

如果日期为26/02/2013日,则您要输入Y / D-1 / M:

new Date(spl[2],spl[0]-1,spl[1]);

从一天中减去1没有任何意义,因此我想您只是弄乱了索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM