[英]Jquery if Condition that will call an Alert if string not in proper format
基本上,我正在尝试制作一个if条件,如果不以正确的格式输入日期,将提醒用户,即格式(2011-09-20)它会提醒用户这个事实。 提前致谢!
只需使用正则表达式 (然后检查它是否可以解析为实际日期)
var testValue = "2011-09-91";
if(!testValue.match(/^[0-9]{4}(\-[0-9]{2}){2}$/) || isNaN(new Date(testValue)))
{
alert("failure");
}
也许是这样的:
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.