I have this field:
public decimal Price { get; set; }
in Database it is decimal (7,2).
View:
@Html.EditorFor(model => model.Price,
new { htmlAttributes = new { @class = "form-control" } })
If i put a value with comma, MVC default validation doesn't accept, says: "The field must be a number"
. (I tried use a Regex, but no way)
For example: 5,00, 55,00 or 555,00
Also this:
public DateTime date { get;set; }
View:
@Html.EditorFor(model => model.Date,
new { htmlAttributes = new { @class = "form-control" } })
MVC default validation doesn't accept dates in format dd/mm/yyyy
, only in mm/dd/yyyy
.
For example: 13/02/2015, 15/06/2013, 25/08/2012
Is something with globalization or what? How can I solve this?
One solution I found was to override the validation functions of jquery.validate.js
<script>
$.validator.methods.range = function (value, element, param) {
var globalizedValue = value.replace(",", ".");
return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
return this.optional(element) || /-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
//Date dd/MM/yyyy
$.validator.methods.date = function (value, element) {
var date = value.split("/");
return this.optional(element) || !/Invalid|NaN/.test(new Date(date[2], date[1], date[0]).toString());
}
</script>