jQuery验证引擎:更改日期格式 [英] jQuery Validation Engine : Change Date Format

查看:101
本文介绍了jQuery验证引擎:更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Datepicker代码

$(function(){ 

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

HTML输入:

<input type="text" class="form-control validate[required,custom[date]]" name="task_start_date" id="task_start_date" />

正确日期输入确认的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"
                },

这里正则表达式期望 YYYY-MM-DD(2014-11-26) / code>格式,但我们使用 dd-M-yy(01-MAR-12)格式。

正则表达式尝试:

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

上述正则表达式没有工作。如何更改 regex 以匹配预期的datepicker输出。在DB中插入的值是 01-MAR-12 而不是 2014-11-26

The above regex did not work. How can I change the regex to match the expected datepicker output. The value inserted in DB is 01-MAR-12 and not 2014-11-26.

谢谢。

推荐答案


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

^(0?[1-9]|[12][0-9]|3[01])[-][A-Z]{3}[-]\d{2}$

这应该是诀窍。您尝试的正则表达式的格式为dd / mm / yyyy或dd-mm-yyyy。

This should do the trick. The regex you tried was for the format of dd/mm/yyyy or dd-mm-yyyy.

这篇关于jQuery验证引擎:更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆