0

I am having issues with html.textboxfor when i have to insert a decimal into my db, which is also set to a decimal.

i have tried this:

[RegularExpression(@"^[0-9]+(\.[0-9]{1,3})$", ErrorMessage = "Valid Decimal number with maximum 3 decimal places.")]

but it will only accept it if i have at least 3 decimals (1,123).

i need it to be able to accept, {1 - 1,2 - 1,23 - 1,234}

how do i achieve this ?

i have not been able to find a regularexpression generator which i could figure out how to use..

or am i in completely the wrong direction as to how i am going to solve my issue?

my value in the model:

 [RegularExpression(@"^[0-9]+(\.[0-9]{1,3})$", ErrorMessage = "Valid Decimal number with maximum 3 decimal places.")]
        [Required]
        public decimal Average { get; set; }

input html in my form:

<div class="form-group">
        @Html.LabelFor(x => x.Average, "Gennemsnit")
        @Html.TextBoxFor(x => x.Average, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.Average, "", new { @class = "text-danger" })@
    </div>
andrelange91
  • 1,018
  • 3
  • 21
  • 48
  • 2
    You seem to miss a `?` after `)` to make the fractional part optional: `@"^[0-9]+(\.[0-9]{1,3})?$"`. Note that you are using `,` as a decimal separator in your examples, and in the regex, you have a dot. – Wiktor Stribiżew Apr 10 '17 at 08:22
  • does not work for what i need, it errors when i only have 1 decimal or 2, it only accepts 3 decimals or none.. – andrelange91 Apr 10 '17 at 08:26
  • Works fine for me... https://regex101.com/r/m9JfOc/1 – Tamás Szabó Apr 10 '17 at 08:27
  • maybe html.textboxfor also validates on the fact that "Average" is a decimal in my model ? – andrelange91 Apr 10 '17 at 08:29
  • 1
    You would need to reconfigure the `$.validator` if your decimal separator in ` comma (by default, `jquery.validate.js` validates numbers with the dot as a decimal separator. –  Apr 10 '17 at 08:32
  • hmm.. should i post a new question to this or whould you have any idea for how to solve this ? – andrelange91 Apr 10 '17 at 08:34
  • [Here](https://forums.asp.net/p/1697138/4493956.aspx) is something that might help. – Wiktor Stribiżew Apr 10 '17 at 08:36
  • @andrelange91. Refer [these answers](http://stackoverflow.com/questions/5199835/mvc-3-jquery-validation-globalizing-of-number-decimal-field) for some options –  Apr 10 '17 at 08:44
  • @StephenMuecke if you add $.validator thingy as answer, i will mark it as such. It was indeed what i needed to do. – andrelange91 Apr 10 '17 at 09:34
  • @andrelange91, You have added an answer in the question (which is not acceptable). Just add you own answer (and delete it from the question) to close this out. –  Apr 10 '17 at 09:48
  • @StephenMuecke i wanted to give mark your response as the answer. but you only left a comment. – andrelange91 Apr 10 '17 at 09:59

1 Answers1

0

Update

It was indeed as StephenMuecke said, that i needed to change $.validator in jquery validator.

so i added this:

(function () {

$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:.\d{3})+)?(?:\,\d+)?$/.test(value);
}
})();

to overwrite the method.

andrelange91
  • 1,018
  • 3
  • 21
  • 48