Javascript Jquery if条件,如果字符串格式不正确,将调用警报

Javascript Jquery if条件,如果字符串格式不正确,将调用警报,javascript,jquery,regex,alert,control-flow,Javascript,Jquery,Regex,Alert,Control Flow,基本上,我正在尝试设计一个if条件,如果用户没有以正确的格式输入日期,即格式(2011-09-20),它将提醒用户这一事实。提前谢谢 只需使用a(然后检查是否可以将其解析为实际日期) 也许是这样的: 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

基本上,我正在尝试设计一个if条件,如果用户没有以正确的格式输入日期,即格式(2011-09-20),它将提醒用户这一事实。提前谢谢

只需使用a(然后检查是否可以将其解析为实际日期)


也许是这样的:

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');

如果想要过滤掉像2011-99-99这样的东西,我们可以做更多的事情,这将接受…更新以处理符合正则表达式的无效日期。
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');