jQuery / JavaScript:是日期吗? (验证是否为日期) [英] jQuery/JavaScript: Is it a date? (Validate whether is a date)
问题描述
我有日期标记,但如果用户的条目是日期,我找不到验证方式,如果遵循所需的格式(格式:yyyy-mm-dd )
I have datepicker, but I could not find a way to validate if the user's entry is a date or not AND if it follows the required format (format: yyyy-mm-dd)
这是我的datepicker:
This is my datepicker:
$("input[name='date']").datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, numberOfMonths: 3, showButtonPanel: true});
我查看了这个解决方案,如何验证datepicker禁止/拒绝某些日期?。它看起来很简单,但它只检查是否是一个周末。它不会检查用户输入的日期,随机数或打字错误(1231 ... 30-30-2011 ... 30/30/2011 ... 212-565-565 ...等) 。
I looked on this solution, "How to validate datepicker to forbid/reject certain dates?". It looks simple enough, but it only checks whether it's a weekend. It does not checks it the user entered a date, a random number or a typo (1231....30-30-2011...30/30/2011...212-565-565...etc.).
请小调我的小提琴: http:// jsfiddle .net / MDa4A /
附加信息(11-10-2011):
(尝试这些jsFiddles并输入0-0-0或0/0/0)
Additional info (11-10-2011):
(Try these jsFiddles and type 0-0-0 or 0/0/0)
我用你的答案来与解决方案。当我使用格式yy-mm-dd时,它会起作用: http:// jsfiddle .net / LyEvQ /
I used your answers to come up with a solution. It works when I use the format "yy-mm-dd": http://jsfiddle.net/LyEvQ/
但是当我使用不同的格式(Ej .:mm / dd / yy)时,它是完全没用的: http://jsfiddle.net/LyEvQ/1/
BUT it is totally useless when I use a different format (Ej.: "mm/dd/yy"): http://jsfiddle.net/LyEvQ/1/
我需要两个工作。你可以请帮助
I need both to work. Can you please help
推荐答案
if (Date.parse("some string")) {
//Valid date
} else {
//Not a valid date
}
这篇关于jQuery / JavaScript:是日期吗? (验证是否为日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!