Hi everyone, in my app I have modified my script files in order to allow comma as decimal separator instead of using a dot (issue related to localization).
This image goes only as explanation.
The thing is, when I use the unmodified script (the min version) the validation works fine... the error messages displays instantaneously and dissapear also instantaneously on correct. But when I use the modified version (non min version), the error messages
just won't show up until I hit the submit button, then the MVC flow occurs (controller->action->view) and the ModelState.isValid goes false, display the same view with the model and obviously the model errors added, with the unobtrusive message sided to the
incorrect field. I then correct the value, I know the value is fine, but the message doesn't clear. With the error message still on screen I hit on submit, then the MVC cycle occurs without errors, with a successful end.
Does any of you know how to fix this? I don't know if I should generate a min version or extend that min version overwriting the function. I don't even know how to do another thing different from the one I've done.
You ust set both client side and server side validation properly, that is bit must conform to the same culture. moreover modifying the validation library is not a good idea...Moreover it is not enough to chenge the regular expression...you must change also
all other nuneric validation attributes to accept comma. So you need a globalization library.
Read this post of my blog to understand how to handle properly globalization issues:
Finally the solution was adding to the script references the one that points to the modified script file, adding it at the end of the references in order to overwrite the .min file behaviour.
1st and 2nd lines are the original references, which respective files remain unchanged.
3rd line is added by myself, thus referencing the file containing the modified function.
Marked as answer by Juan Cabral on Dec 31, 2012 11:02 PM
Setting up server side validation wouldn't do the trick because I need that users from anywhere in the world be able to use my site, but the server still will know one and only one way to manage decimal values, dots or commas. I wanted to get a solution
without using internationalization plugins and stuff like that.
It is not wise to avoid using globalization plugins. If in future version of your software you need to manipulate some numeric value entered by the user on the client side you will need a globalization plugin, your trick will not help parsing floats in the
right way. Moreover, with your solution the RangeAttribute WILL NOT WORK on the client side(it requires number parsing on the client side), so if you will need using it (maybe in a future modification of the software) you CANNOT DO IT. It is better to use
a globalization plugin from the beginning possibly using just one single culture that you can set manually.
Juan Cabral
0 Points
3 Posts
Unobtrusive validation ASP.NET MVC3
Dec 01, 2012 07:04 PM|LINK
Hi everyone, in my app I have modified my script files in order to allow comma as decimal separator instead of using a dot (issue related to localization).
This image goes only as explanation.
The thing is, when I use the unmodified script (the min version) the validation works fine... the error messages displays instantaneously and dissapear also instantaneously on correct. But when I use the modified version (non min version), the error messages just won't show up until I hit the submit button, then the MVC flow occurs (controller->action->view) and the ModelState.isValid goes false, display the same view with the model and obviously the model errors added, with the unobtrusive message sided to the incorrect field. I then correct the value, I know the value is fine, but the message doesn't clear. With the error message still on screen I hit on submit, then the MVC cycle occurs without errors, with a successful end.
The Views references this scripts in two lines:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>So, I look up for the non-min versions, jquery.validate.js is the one holding the function I need to change.
The original function is as follows (uses dot as decimal separator):
// http://docs.jquery.com/Plugins/Validation/Methods/number number: function(value, element) {return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
},
I modified it as follows (to use only comma as decimal separator):
// http://docs.jquery.com/Plugins/Validation/Methods/number number: function(value, element) { return this.optional(element) || /^-?(?:\d+)(?:[\,]\d+)?$/.test(value); },Then I changed the references to the modified version of the script:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>Does any of you know how to fix this? I don't know if I should generate a min version or extend that min version overwriting the function. I don't even know how to do another thing different from the one I've done.
Any help would be great!
Thanks in advance!
francesco ab...
All-Star
20954 Points
3286 Posts
Re: Unobtrusive validation ASP.NET MVC3
Dec 01, 2012 07:56 PM|LINK
You ust set both client side and server side validation properly, that is bit must conform to the same culture. moreover modifying the validation library is not a good idea...Moreover it is not enough to chenge the regular expression...you must change also all other nuneric validation attributes to accept comma. So you need a globalization library.
Read this post of my blog to understand how to handle properly globalization issues:
http://www.dotnet-programming.com/post/2011/12/14/Globalization-Validation-and-DateNumber-Formats-in-AspNet-MVC.aspx
Mvc Controls Toolkit | Data Moving Plug-in Videos
Juan Cabral
0 Points
3 Posts
Re: Unobtrusive validation ASP.NET MVC3
Dec 31, 2012 11:02 PM|LINK
Finally the solution was adding to the script references the one that points to the modified script file, adding it at the end of the references in order to overwrite the .min file behaviour.
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>1st and 2nd lines are the original references, which respective files remain unchanged.
3rd line is added by myself, thus referencing the file containing the modified function.
Juan Cabral
0 Points
3 Posts
Re: Unobtrusive validation ASP.NET MVC3
Dec 31, 2012 11:06 PM|LINK
Setting up server side validation wouldn't do the trick because I need that users from anywhere in the world be able to use my site, but the server still will know one and only one way to manage decimal values, dots or commas. I wanted to get a solution without using internationalization plugins and stuff like that.
francesco ab...
All-Star
20954 Points
3286 Posts
Re: Unobtrusive validation ASP.NET MVC3
Jan 01, 2013 02:04 AM|LINK
It is not wise to avoid using globalization plugins. If in future version of your software you need to manipulate some numeric value entered by the user on the client side you will need a globalization plugin, your trick will not help parsing floats in the right way. Moreover, with your solution the RangeAttribute WILL NOT WORK on the client side(it requires number parsing on the client side), so if you will need using it (maybe in a future modification of the software) you CANNOT DO IT. It is better to use a globalization plugin from the beginning possibly using just one single culture that you can set manually.
Mvc Controls Toolkit | Data Moving Plug-in Videos