开发者

Jquery if Condition that will call an Alert if string not in proper format

Basically, I am trying to craft an if condition 开发者_运维技巧that will alert users if the do not enter their date in the proper format, i.e. the format (2011-09-20) it will alert users to this fact. Thanks in advance!


Just use a regular expression (and then check if it can be parsed into an actual date)

var testValue = "2011-09-91";

if(!testValue.match(/^[0-9]{4}(\-[0-9]{2}){2}$/) || isNaN(new Date(testValue)))
{
    alert("failure");
}


Maybe something like this:

if ('2011-09-20'.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/))
    alert('this is maybe a date in the correct format');
else
    alert('this is not the correct format');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜