Jquery 日期选择器 Chrome

发布于 2024-11-06 16:02:17 字数 424 浏览 6 评论 0原文

在使用 jQuery UI Datepicker 时,我们在 Google Chrome 中使用时遇到一个问题: 当我们输入大于 12 的日期时,它不接受它作为有效日期,这是因为 chrome 认为日期格式是 mm/dd/yyyy。我们尝试通过添加代码来尝试将日期设置强制为 dd/mm/yyyy 来解决

$('.date').datepicker({ dateFormat: "dd/mm/yy" });

此问题是否有任何方法可以解决此问题,以便我们的日期选择器将接受 dd/mm/yyyy 值? 我们仅在 google Chrome 中遇到此问题,日期修复适用于 Firefox、ie 和 Firefox。野生动物园。 我们正在使用 ASPX & MVC3 与这个项目。

如果有人可以解决我们的问题,那就太好了,

谢谢

When using jQuery UI Datepicker, we encouter a problem when used in Google Chrome:
when we enter a date with a day higher than 12, it does not accept it as a valid date, this is because chrome thinks the dateformat is mm/dd/yyyy. We tried to solve this by adding code to try to force the date settings into dd/mm/yyyy

$('.date').datepicker({ dateFormat: "dd/mm/yy" });

Is there any way to resolve this issue, so our datepicker will accept dd/mm/yyyy values?
We only have this issue in google Chrome, the datefix works for firefox, ie & safari.
We are using ASPX & MVC3 with this project.

If someone could solve our issue, that would be great

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

姐不稀罕 2024-11-13 16:02:18

我遇到了同样的问题,并且与所有基于 Webkit 的网络浏览器有关。如果您设置大写 M,文本框将显示带有字母的飞蛾。对我来说最好的解决方案是覆盖 jquery.validate.js 中的验证日期函数

创建 jquery.validate.date.js 并确保它在 jquery.validate.js 之后加载

将以下代码添加到 jquery.validate.date.js

$(function() {
    $.validator.methods.date = function (value, element) {
        if ($.browser.webkit) {

            //ES - Chrome does not use the locale when new Date objects instantiated:
            var d = new Date();

            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
        }
        else {

            return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
        }
    };
});

I have had the same problem and is related with all Webkit based web browsers. If you set uppercase M the textbox show the moth with letters. The best solution for me was to override the validate date function from jquery.validate.js

Create jquery.validate.date.js and ensure it is loaded after jquery.validate.js

Add the following code to jquery.validate.date.js

$(function() {
    $.validator.methods.date = function (value, element) {
        if ($.browser.webkit) {

            //ES - Chrome does not use the locale when new Date objects instantiated:
            var d = new Date();

            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
        }
        else {

            return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
        }
    };
});
你的笑 2024-11-13 16:02:18

user1011138 解决方案对我不起作用,因为 .toLocaleDateString(value) 无法解析 value 字符串,

这是我想出的解决方案 =>
在 jquery.validate.js 中找到此函数定义:“date: function (value, element)”并将代码替换为:

// http://docs.jquery.com/Plugins/Validation/Methods/date
date: function (value, element) {
    var d = value.split("/");
    return this.optional(element) || !/Invalid|NaN/.test(new Date((/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) ? d[1] + "/" + d[0] + "/" + d[2] : value));
},

user1011138 solution dont work for me since .toLocaleDateString(value) doesn't parse the value string

here's the solution i came up with =>
in jquery.validate.js find this function definition: "date: function (value, element)" and replace the code with:

// http://docs.jquery.com/Plugins/Validation/Methods/date
date: function (value, element) {
    var d = value.split("/");
    return this.optional(element) || !/Invalid|NaN/.test(new Date((/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) ? d[1] + "/" + d[0] + "/" + d[2] : value));
},
夕嗳→ 2024-11-13 16:02:18

上述解决方案不起作用,因为 jquery 浏览器检查已被弃用,抛出 jquery.validate.min.js Uncaught TypeError:无法读取未定义的属性“webkit”。检查元素 DateOfBirth 时发生异常,请检查“日期”

在我的项目中,我将 momentjsbootstrap datetimepicker,所以这个解决方案效果很好:

 $(function () {
    $.validator.methods.date = function (value, element) {
        return this.optional(element) || moment(value, 'DD/MM/YYYY').isValid();
  };
});

加载 jquery.validate() 后立即调用它

The above solutions didn't work because jquery browser check is deprecated throwing jquery.validate.min.js Uncaught TypeError: Cannot read property 'webkit' of undefined. Exception occurred when checking element DateOfBirth, check the 'date'

In my project, I use momentjs with bootstrap datetimepicker, so this solution works great:

 $(function () {
    $.validator.methods.date = function (value, element) {
        return this.optional(element) || moment(value, 'DD/MM/YYYY').isValid();
  };
});

Call this right after loading jquery.validate()

舞袖。长 2024-11-13 16:02:18

您必须使用“en-GB”日期验证覆盖默认的“en-US”日期验证。

  • 'en-US' => '月/日/年'
  • 'en-GB' => 'dd/mm/yy'

解决方案:

在您的项目中添加一个“jquery.validate.date.js”文件,并将以下代码放入其中:

//To Fix jQuery date format 'en-GB' validation problem in Chrome
$(function () {
    $.validator.addMethod(
        "date",
        function (value, element) {
            var bits = value.match(/([0-9]+)/gi), str;
            if (!bits)
                return this.optional(element) || false;
            str = bits[1] + '/' + bits[0] + '/' + bits[2];
            return this.optional(element) || !/Invalid|NaN/.test(new Date(str));
        },
        "Please enter date in valid format [dd/mm/yyyy]"
    );
});

并确保它在“jquery.validate”之后加载.min.js':

<script type="text/javascript" src="/Scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.date.js"></script>

You've got to override default 'en-US' date validation with 'en-GB' date validation.

  • 'en-US' => 'mm/dd/yy'
  • 'en-GB' => 'dd/mm/yy'

Solution:

add a "jquery.validate.date.js" file in your project and put the following code in it:

//To Fix jQuery date format 'en-GB' validation problem in Chrome
$(function () {
    $.validator.addMethod(
        "date",
        function (value, element) {
            var bits = value.match(/([0-9]+)/gi), str;
            if (!bits)
                return this.optional(element) || false;
            str = bits[1] + '/' + bits[0] + '/' + bits[2];
            return this.optional(element) || !/Invalid|NaN/.test(new Date(str));
        },
        "Please enter date in valid format [dd/mm/yyyy]"
    );
});

and make sure it load after the 'jquery.validate.min.js':

<script type="text/javascript" src="/Scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.date.js"></script>
指尖上得阳光 2024-11-13 16:02:18

创建一个新的 jquery.validate.date.js 文件。

将以下代码粘贴到文件中。

 $(function () {
    $.validator.methods.date = function (value, element) {
      if ($.browser.webkit) {
        var d = new Date();
        return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
      }
      else {
        return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
    }
  };
});

现在确保该文件在 jquery.validate.js 文件之后加载。

Create a new jquery.validate.date.js file.

Paste the following code inside the file.

 $(function () {
    $.validator.methods.date = function (value, element) {
      if ($.browser.webkit) {
        var d = new Date();
        return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
      }
      else {
        return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
    }
  };
});

Now ensure that this file is loaded after jquery.validate.js file.

坚持沉默 2024-11-13 16:02:18
 $.validator.methods.date = function (value, element) {                
            if ($.browser.webkit) {                    
                var d = value.split("/");   
                return this.optional(element) || !/Invalid|NaN/.test(new Date((/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) ? d[1] + "/" + d[0] + "/" + d[2] : value));
              }
            else {
                return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
            }
        };
 $.validator.methods.date = function (value, element) {                
            if ($.browser.webkit) {                    
                var d = value.split("/");   
                return this.optional(element) || !/Invalid|NaN/.test(new Date((/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) ? d[1] + "/" + d[0] + "/" + d[2] : value));
              }
            else {
                return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
            }
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文