2

My server converts float into a string using comma and then jquery alerts, that this float's format is incorrect (it requires dot). I'm not familiar with this auto generated jquery validation script and I don't know where to change it, so I'm asking here.

Here's my code and a pic of alert.

    @model NerdDinner.Models.Dinner

    (...)

    <div class="editor-label">
        @Html.LabelFor(model => model.Longitude)
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.Longitude)
        @Html.ValidationMessageFor(model => model.Longitude)
    </div>

enter image description here

Thanks.

Patryk
  • 3,042
  • 11
  • 41
  • 83
  • It's unclear what you're asking -- do you want to change the (jQuery) client or the ASP.NET server? (and please retag your question based on which!) – Christian Ternus Oct 27 '13 at 18:23
  • well, it would be cool to know both of these – Patryk Oct 27 '13 at 18:24
  • There's no way we can help you fix either unless we can see the code for them. – Christian Ternus Oct 27 '13 at 18:25
  • There is only an object created by LINQ (Dinner - auto generated class) and it's float properties (Latitude, Longitude). I didn't even ask visual studio to add validation for me, it did it on it's own. I wasn't implementing any validation myself. – Patryk Oct 27 '13 at 18:29
  • I think, you are looking for one of the answers here (might not want to modify it globally though): http://stackoverflow.com/questions/13086494/asp-net-mvc-set-number-format-default-decimal-thousands-separators – complex857 Oct 27 '13 at 18:33

2 Answers2

0

With javascript you can take those values ​​and convert into float doing a replace of "," by ".":

var Longitude = parseFloat($("#longitude").val().replace(",", "."));
var Latitude= parseFloat($("#latitude").val().replace(",", "."));
parsley72
  • 8,449
  • 8
  • 65
  • 98
0

As Steven De Waele has mentioned in his post at Validating numbers with comma’s with jQuery, you can override the methods of the jQuery validation plugin to achieve your goal.

$.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);
}
Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23