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');
精彩评论