繁体   English   中英

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

[英]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.

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