I'm trying to use the simplest possible scenario using a date picker in different browsers. I suspect I'm doing something very simple the wrong way but after lots of searching around I still haven't found a solution. Below is some sample code that represents what I'm attempting.
我尝试在不同的浏览器中使用日期选择器来使用最简单的场景。我怀疑我做了一些非常简单的错误的方法,但在很多搜索之后,我仍然没有找到一个解决方案。下面是一些代表我正在尝试的示例代码。
If I use Chrome (v12.0.742.122) and pick a date from the picker like 13/08/2011 the jQuery validation logic will not allow the page to submit even though I've explicitly specified the format as 'dd/mm/yy'.
如果我使用Chrome (v12.0.742.122)并从选择器中选择一个日期,比如13/08/2011,jQuery验证逻辑将不允许页面提交,尽管我已经明确指定了格式为'dd/mm/yy'。
If I change the format to 'dd/M/yy' and choose a date like 13/Aug/2011 it works in Chrome but then won't submit for me in IE (v8.0.7600.16385). In FireFox (v3.6.18) both formats work.
如果我将格式更改为'dd/M/yy',并选择一个日期,比如13/ 8 /2011,它可以在Chrome中工作,但不会在IE中为我提交(v8.0.7600.16385)。在FireFox中(v3.6.18)两种格式都可以。
What validation script do I need to be able to support date formats of 'dd/mm/yy' in Chrome?
我需要什么样的验证脚本才能支持Chrome中'dd/mm/yy'的日期格式?
Date:
50
Four hours later I finally stumbled across the answer. For some reason Chrome seems to have some inbuilt predilection to use US date formats where IE and FireFox are able to be sensible and use the regional settings on the OS.
四个小时后,我终于找到了答案。出于某种原因,Chrome似乎更倾向于使用美国日期格式,IE和火狐都可以在操作系统上使用地区设置。
jQuery.validator.methods["date"] = function (value, element) { return true; }
48
You can use the Globalize.js plugin available here: https://github.com/jquery/globalize
您可以使用Globalize。js插件在这里可用:https://github.com/jquery/globalize。
Then simply redefine the validate method in your web site's main script file like so (avoid editing the jquery.validate.js library file as you may want to update it in future):
然后在web站点的主脚本文件中重新定义验证方法(避免编辑jquery.validate)。js库文件,您可能想在将来更新它):
$.validator.methods.date = function (value, element) {
return this.optional(element) || Globalize.parseDate(value, "d/M/y", "en");
}
20
I know I'm a bit late to the party, but here's the solution I used to Chrome validation not accepting UK format dates. A simple plug and play validation, it doesn't rely on any plugins or modifying any files. It will accept any date between 1/1/1900 and 31/31/2999, so US or UK format. Obviously there are invalid dates that will sneak past, but you can tweak the regex to your heart's content. This met my needs as it will be used on a low traffic area of the site, with server-side validation. The area of the site in question is built with ASP.net MVC.
我知道我在聚会上有点晚了,但这是我用Chrome验证的解决方案,不接受英国格式的日期。一个简单的即插即用验证,它不依赖于任何插件或修改任何文件。它将接受1/1900和31/2999之间的任何日期,也就是US或UK格式。很明显,有一些无效的日期会悄悄过去,但是你可以把正则表达式调整到你的内心。这满足了我的需求,因为它将用于站点的低流量区域,并进行服务器端验证。该站点的相关区域是使用ASP.net MVC构建的。
jQuery.validator.methods.date = function(value, element) {
var dateRegex = /^(0?[1-9]\/|[12]\d\/|3[01]\/){2}(19|20)\d\d$/;
return this.optional(element) || dateRegex.test(value);
};
18
Looks like this issue has been raised with the JQuery team.
看起来这个问题已经被JQuery团队提出了。
https://github.com/jzaefferer/jquery-validation/issues/153
https://github.com/jzaefferer/jquery-validation/issues/153
Looks like the workaround for now is to use dateITA in additional-methods.js
看起来目前的解决方案是在additional-methods.js中使用dateITA
It looks like this:
它看起来像这样:
jQuery.validator.addMethod(
"dateITA",
function(value, element) {
var check = false;
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if( re.test(value)){
var adata = value.split('/');
var gg = parseInt(adata[0],10);
var mm = parseInt(adata[1],10);
var aaaa = parseInt(adata[2],10);
var xdata = new Date(aaaa,mm-1,gg);
if ( ( xdata.getFullYear() == aaaa )
&& ( xdata.getMonth () == mm - 1 )
&& ( xdata.getDate() == gg ) )
check = true;
else
check = false;
} else
check = false;
return this.optional(element) || check;
},
"Please enter a correct date"
);
If you add that validator you can then attach your datepicker to the '.dateITA' class.
如果您添加了验证器,那么您可以将您的datepicker附加到'。dateITA”类。
Thus far this has worked well for me to get me beyond this stupid issue in Chrome.
到目前为止,这对我来说很有效,让我摆脱了Chrome这个愚蠢的问题。
2
This remains a problem in .net mvc4, where the EditorFor
DateTime
still produces a data-val-date
attribute, despite clear documentation in the jQuery validation plugin not to use it!!! Hopefully microsoft fixes this and data-val-date
is never heard of again!
在。net mvc4中,这仍然是一个问题,DateTime的编辑器仍然生成一个data- valdate属性,尽管jQuery验证插件中有明确的文档说明不要使用它!希望微软能修复这个问题,并且再也不会有人听说过数据值日期了!
In the meantime you could use the suggested library via modernizr:
与此同时,您可以通过modernizr使用建议库:
yepnope({
test: isNaN(Date.parse("23/2/2012")),
nope: 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.min.js',
complete: function () {
$.validator.methods.date = $.validator.methods.dateITA
}
});
or if not wanting to use yepnope/modernizr, and to get rid of the UTC component of the dateITA method (if using this library to enter a local time, dates will not always validate on the 1st or last day of the month unless on the Greenwich line - i.e. UTC +0):
如果不想使用yepnope / modernizr,并摆脱UTC dateITA方法的组件(如果使用这个库进入当地时间,日期不会总是验证一日或月的最后一天,除非在格林威治行——即UTC + 0):
(function ($) {
var invokeTestDate = function () {
return $.validator.methods.date.call({
optional: function () {
return false;
}, (new Date(2012,8,23)).toLocaleDateString(), null);
};
if (invokeTestDate()) {
return;
}
// http://docs.jquery.com/Plugins/Validation/Methods/date
$.validator.methods.date = function (value, element) {
//ES - Chrome does not use the locale when new Date objects instantiated:
//return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
})(jQuery);
2
We use the following to work on our projects;
我们使用下面的方法来完成我们的项目;
if ($.validator) {
$.validator.addMethod("date",
function (value, element, params) {
if (this.optional(element)) {
return true;
}
var ok = true;
try {
$.datepicker.parseDate("dd/mm/yy", value);
}
catch (err) {
ok = false;
}
return ok;
});
}
1
May this code help you.
希望这段代码对您有所帮助。
$.validator.addMethod(
"date",
function(value, element) {
var check = false;
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
var reBR = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
if( re.test(value)){
var adata = value.split('/');
var gg = parseInt(adata[0],10);
var mm = parseInt(adata[1],10);
var aaaa = parseInt(adata[2],10);
var xdata = new Date(aaaa,mm-1,gg);
if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) )
check = true;
else
check = false;
} else if( reBR.test(value)){
var adata = value.split('-');
var aaaa = parseInt(adata[0],10);
var mm = parseInt(adata[1],10);
var gg = parseInt(adata[2],10);
var xdata = new Date(aaaa,mm-1,gg);
if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) )
check = true;
else
check = false;
} else
check = false;
console.log(check);
return this.optional(element) || check;
},
"Por favor insira uma data válida"
);
0
should change the dateRule method you define in validate to (the regex is just a simple one for example):
应该更改在validate to中定义的dateRule方法(例如,regex只是一个简单的方法):
$.validator.addMethod(
"dateRule",
function(value, element) {
return value.match(/^\d\d?\/\d\d?\/\d\d$/);
},
"Please enter a date in the format dd/mm/yy"
);
you can switch the regex for whatever you wish. I got this regex for date from here which supports M/D/YY or M/D/YYYY or MM/DD/YYYY or MM/DD/YY: 1/1/1920 through 12/31/2019; Feb 29 and 30 always allowed
您可以随意切换regex。我从这里得到的这个regex支持M/D/YY或M/D/YYYY或MM/DD/ yyy或MM/DD/YY: 1/1/1920到12/31/2019;2月29日和30日总是允许的
^((0?[13578]|10|12)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1}))|(0?[2469]|11)(-|\/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))(-|\/)((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1})))$
0
I also found a solution that appears to work for any culture.
我还找到了一个似乎适用于任何文化的解决方案。
http://devdens.blogspot.com/2011/11/jquery-validation-fix-for-date-format_29.html
http://devdens.blogspot.com/2011/11/jquery-validation-fix-for-date-format_29.html
It requires you to modify the actual jquery.valdiation.min.js file, but so far it's working for me.
它要求您修改实际的jquery.valdi .min。js文件,但是到目前为止,它对我来说是有用的。
0
This is the only solution that worked for me: http://www.codeproject.com/Tips/579279/Fixing-jQuery-non-US-Date-Validation-for-Chrome
这是唯一对我有用的解决方案:http://www.codeproject.com/tips/579279 / fixing-jquer-non - us - datevalidfor chrome
jQuery.extend(jQuery.validator.methods, {
date: function (value, element) {
var isChrome = window.chrome;
// make correction for chrome
if (isChrome) {
var d = new Date();
return this.optional(element) ||
!/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
// leave default behavior
else {
return this.optional(element) ||
!/Invalid|NaN/.test(new Date(value));
}
}
});
0
I found the simplest correction of this error to be the following code snippet after calling your validation file;
我发现这个错误最简单的修正是在调用您的验证文件后的代码片段;
jQuery.validator.methods["date"] = function (value, element){
var shortDateFormat = "dd/mm/yy";
var res = true;
try {
$.datepicker.parseDate(shortDateFormat, value);
} catch (error) {
res = false;
}
return res;
}
Even in versions found in 2016 im still having this issue without the above code in Chrome and not Firefox.
即使是在2016年发现的版本中,我仍然有这个问题,没有以上的代码在Chrome而不是Firefox。
This is my preferred solution as it does not require any additional libraries or plugins to work.
这是我首选的解决方案,因为它不需要任何额外的库或插件来工作。
I found this code snippet while googling the issue here.
我在这里搜索问题时找到了这个代码片段。
0
Or we might try to overwrite the validator instead of ignoring it.
或者我们可以尝试覆盖验证器,而不是忽略它。
$.validator.addMethod("date", function (value, element) {
var result = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
result = false;
}
return result;
});