2011-04-27 25 views
0

如何编写一个日期格式验证函数,通过该函数我可以验证日月年月日。日期格式为(mon-yyyy),如Apr-2011或其他。 我需要验证使用jQuery.How编写这种验证功能? 我已经有一个日期选择器。我只想知道如何在提交日期格式之前验证表单。JQuery DateFormat验证(Mon-YYYY)?

+0

http://stackoverflow.com/questions/4852817/validation-of-invalid-date-leap-year – Rafay 2011-04-27 06:54:51

回答

1
//Well, though this is a repeated question, 
//just to quench yo thirst, you can use javascript's RegExp for regular expressions. 
//you can create one like: 
var RegularExpression = new RegExp("pattern"); 
//or 
var RegularExpression2 = /pattern/; 

//check for valid date format as: 
var datePattern = /^\w{3}[-]\d{4}$/ 
//for numerical month value, u could use 
//var datePattern = /^\d{1,2}[-]\d{4}$/ 
if(datePattern.test('my_date_string')) 
    alert('correct') 
else 
    alert('false') 

//or in a jquery way, if your date-holding element is an input with id as dateId 
//then you can do: 

if(datePattern.test($('#dateId').val())) 
    alert('correct') 
else 
    alert('false') 
+0

谢谢!!!有效。我无法找到所发布的内容。不管怎么说,多谢拉!! :) – 2011-04-27 07:42:28

+0

不错,接受一个答案然后:-)顺便说一句,为了确定是否确定是否提交表单,然后为了有更多的控制权,避免表单中的默认类型'submit'按钮,而是在'onclick'参数中使用自定义函数,然后根据字段是否通过验证模式,您可以选择在哪个函数中提交该函数。你可以使用jquery提交你的表单 - 如果你不知道如何,将它留作练习:-) – nemesisfixx 2011-04-27 08:03:38