I'm trying to validate user input of type "Double" but i have very strange behavior
Model
public class User
{
public double CodeA { get; set; }
public double CodeB { get; set; }
}
View
<div class="row">
<div class="form-group">
<div class="col-md-1">
<label class="control-label">CodeA</label>
</div>
<div class="col-md-2">
@Html.TextBoxFor(model => model.CodeA, new { @class = "form-control input-sm cus-read" })
</div>
<div class="col-md-3">
@Html.ValidationMessageFor(model => model.CodeA)
</div>
<div class="col-md-1">
<label class="control-label">CodeB</label>
</div>
<div class="col-md-2">
@Html.TextBoxFor(model => model.CodeB, new { @class = "form-control input-sm cus-read" })
</div>
<div class="col-md-3">
@Html.ValidationMessageFor(model => model.CodeB)
</div>
</div>
</div>
Input are CodeA = "1,09" and CodeB = "2,11" When forms are submitted, i got error message for CodeA "Input must be numeric" but no error for CodeB??
What i did so far i did add the following: in web.config
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="fr-BE"
uiCulture="fr-BE"
/>
</system.web>
I also read here that i have to also include client side validation: Found Here
where should i add this file? I added before and after the call of JqueryValidate but nothing happened? should i copy the code and added to jquery.validate.js file? I would like to add this file : https://github.com/jquery-validation/jquery-validation/blob/master/src/localization/methods_nl.js
I also used ModelBinder class with no success:
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
ModelState modelState = new ModelState { Value = valueResult };
object actualValue = null;
try
{
actualValue = Convert.ToDouble(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
I don't know what am i doing wrong?
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function() { // will trigger when the document is ready
$('.datepicker').datepicker({
format: "dd/mm/yy",
todayBtn: "linked",
autoclose: true,
todayHighlight: true
}); //Initialise any date pickers
});
$.extend($.validator.methods, {
date: function (value, element) {
return this.optional(element) || /^\d\d?[\.\/\-]\d\d?[\.\/\-]\d\d\d?\d?$/.test(value);
}
});
</script>
jqueryval is the validation bunles