jQuery技术:jQueryvalidation引擎:更改日期格式

我的Datepicker代码

$(function(){ $( "#task_start_date").datepicker({ dateFormat: 'dd-M-yy', showOn: 'button', buttonImage :image_us, buttonImageOnly: true }); }); 

HTML输入:

  

用于正确日期输入validation的JS函数:

 "date": { // Check if date is valid by leap year "func": function (field) { var pattern = new RegExp(/^(d{4})[/-.](0?[1-9]|1[012])[/-.](0?[1-9]|[12][0-9]|3[01])$/); var match = pattern.exec(field.val()); if (match == null) return false; var year = match[1]; var month = match[2]*1; var day = match[3]*1; var date = new Date(year, month - 1, day); // because months starts from 0. return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); }, "alertText": "* Invalid date, must be in YYYY-MM-DD format" }, 

这里的regex期望YYYY-MM-DD (2014-11-26)格式,但我们使用的是dd-M-yy(01-MAR-12)格式。

正则表达式尝试:

 /^(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-]d{4}$/ 

上面的regex不起作用。 如何更改regex以匹配预期的datepicker输出。 DB中插入的值为01-MAR-12而不是2014-11-26

谢谢。

    ^(0 [1-9] | [12] [0-9] |?3 [01])[ – ] [AZ] {3} [ – ] d {2} $

    这应该可以解决问题。 你试过的正则表达式是dd / mm / yyyy或dd-mm-yyyy的格式。

    试试这个(未经测试):

     (?:0[1-9]|1[0-9]|2[0-9]|3[01])-(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-d{2} 

    你可以缩小正则表达式,但我现在没有时间。

    使用这一个

     "date": { // Check if date is valid by leap year "func": function (field) { var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[/-.](0?[1-9]|1[012])[/-.](d{4})$/); var match = pattern.exec(field.val()); if (match == null) return false; var year = match[3]; var month = match[2]*1; var day = match[1]*1; var date = new Date(year, month - 1, day); // because months starts from 0. return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); }, "alertText": "* Invalid date, must be in dd-mm-yyyy format" }, 

      以上就是jQuery教程分享jQueryvalidation引擎:更改日期格式相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/532332.html

      (0)
      上一篇 2020年12月21日
      下一篇 2020年12月21日

      精彩推荐