错误日期验证MVC [英] Error in date validation MVC
问题描述
我的模型类的属性看起来像这样
My model class property looks like this
public DateTime PurchaseDate { get; set; }
和内景
@Html.TextBoxFor(model => model.PurchaseDate, new { @class = "form-control date-picker" })
@Html.ValidationMessageFor(model => model.PurchaseDate)
和我给一个日期这样的形式
and I am giving a date like this in form
19/06/2015
但它给验证消息,而不是允许提交页面,留言是这样的。
But it gives validation message and not allows page to be submitted, message is like this
The field PurchaseDate must be a date.
如果我给在 MM / DD / YYYY日期
格式,它的工作原理。任何人都可以指出我在做什么错在这里?
if I give date in mm/dd/yyyy
format it works. Can anyone point out what I am doing wrong here?
推荐答案
客户端错误默认情况下,因为发生 jquery.validate
测试使用<$ C值$ C> MM / DD / YYYY 格式。您可以覆盖 $。validator.addMethod(日期,功能(价值元素)
函数来测试值是在 DD / MM / YYYY
您期望的那样。请注意以下几点code是一个 @ Html.DatePickerFor()
helper方法有关我自己的jQuery插件这使得基于服务器的文化输出数据的日期格式
属性,因此它可能是你的需要矫枉过正
The client side error is occurring because by default jquery.validate
tests the value using the MM/dd/yyyy
format. You can override the $.validator.addMethod('date', function (value, element)
function to test that the value is in the dd/MM/yyyy
you expect. Note the following code is from my own jquery plugin associated with a @Html.DatePickerFor()
helper method which renders a data-dateformat
attribute in the output based on the servers culture, so it may be an overkill for your needs
添加以下脚本(不可以在的document.ready
,但经过 jquery.validate.unobtrusive
)
Add the following scripts (not in document.ready
, but after jquery.validate.unobtrusive
)
Date.prototype.isValid = function () {
return !isNaN(this.getTime());
}
globalDate = function (value, formatString) {
// Initialise a new date
var date = new Date();
date.setHours(0, 0, 0, 0);
if (value == undefined) {
// Return todays date
return date;
}
// Get the components of the format
// The separator can be forward slash, hyphen, dot and/or space
var regex = new RegExp(/([dMy]+)([\s/.-]+)([dMy]+)([\s/.-]+)([dMy]+)/);
//var format = regex.exec(this.inputFormat);
var format = regex.exec(formatString);
// Get the components of the value
regex = new RegExp(/(\d+)([\s/.-]+)(\d+)([\s/.-]+)(\d+)/);
value = regex.exec(value);
// Check the value is valid
if (value === null || value[2] !== format[2] || value[4] !== format[4]) {
// Its not valid
date.setTime(Number.NaN);
return date;
}
// TODO: What if year entered as 2 digits?
var day = Number.NaN;
var month = Number.NaN;
var year = Number.NAN;
if (format[1].charAt(0) === 'd') {
// little-endian (day, month, year)
day = parseInt(value[1]);
month = parseInt(value[3]) - 1;
year = parseInt(value[5]);
} else if (format[1].charAt(0) === 'M') {
// middle-endian (month, day, year)
day = parseInt(value[3]);
month = parseInt(value[1]) - 1;
year = parseInt(value[5]);
} else {
// big endian (year, month, day)
day = parseInt(value[5]);
month = parseInt(value[3]) - 1;
year = parseInt(value[1]);
}
date.setFullYear(year);
date.setMonth(month);
date.setDate(day);
// Check its valid
if (date.getDate() !== day || date.getMonth() !== month || date.getFullYear() !== year) {
date.setTime(Number.NaN);
return date;
}
return date;
}
$.validator.addMethod('date', function (value, element) {
var format = "dd/MM/yyyy";
return this.optional(element) || globalDate(value, format).isValid();
}
如果您只想测试的格式 DD / MM / YYYY
,那么你可以简化 globalDate()
只需使用功能
If you only ever want to test for the format dd/MM/yyyy
, then you could simplify the globalDate()
function by just using
var date = new Date();
date.setHours(0, 0, 0, 0);
var components = value.split('/');
var day = components[0];
var month = components[1];
var year = components[2];
date.setFullYear(year);
....
修改
继OP的有关服务器端验证失败的意见,服务器文化需要接受格式为 DD / MM / YYYY
日期字符串。在 web.config.cs
文件
Further to OP's comments regarding server side validation failing, the server culture needs to accept a date string in the format dd/MM/yyyy
. In the web.config.cs
file
<system.web>
<globalization culture="en-AU" uiCulture="en-AU"/> // adjust to your culture code
....
这篇关于错误日期验证MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!