This project is read-only.

Validating internationalized numbers

Jul 24, 2012 at 11:31 PM

I'm trying to get my website to accept international numbers but I'm having difficulty getting it to work if I apply the Range attribute to my model.  I read the following in the globalization tutorial.

Summing up we may globalize our web application by adding just:

<script type='text/javascript' src="@Url.Content("~/Scripts/globalize.min.js")"></script>



However, this is not enough to have all our validation rules working properly! In fact the client side code of the RangeAttribute still uses the standard javascript parsing functions for numbers and dates instead of their globalized counterparts.

The Mvc Controls Toolkit autodetects the gloabalization library used, if any, and uses its parsing functions in all client side validation rules. Therefore, if we use the Mvc Controls Toolkit the few lines of code I showed before are enough to put in place the globalization of the whole validation library.

I took this to mean that if you use the MVC Controls Toolkit, you can have the Range attribute work with international numbers (such as 4,5 to represent 4 and a half).  But I downloaded the Basic Tutorials Example and added a double property to the ToDo class.  Then I added the Range attribute to my property, but although the property displays in the correct #,# format, there is a validation error when I tab into and out of that text box.  So should this work, or did I misunderstand the blog post and this is a known issue?


Jul 25, 2012 at 12:43 AM

As default the Mvc Controls Toolkit DOESNT change the standard RangeAdapter that is not globalized, since you can use the Mvc Controls Toolkit DynamicRangeAttribute that is globalized and is more powerfull than the standard Range attrinute, sinc it allows also dynamic maximum and minimum. So I suggest to use the DynamicRangeAttribute or also the DateRangeAttribute that is specific for dates.


Anyway if you want the standared RangeAttribute globalized, you can substitute the standard adapter of the mvc with a globalized adapter provided by the Mvc Controls Toolkit in the global asax:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(MVCControlsToolkit.DataAnnotations.RangeAdapter));

Jul 25, 2012 at 5:25 AM

Thanks for the reply.  I tried this.

        [DynamicRange(typeof(double), DynamicMinimum = "0", DynamicMaximum = "24", ErrorMessage = "Must be between 0 and 24")]
        public object Hours { get; set; }

But that causes (model.ToDoList)._items[0].Value to be null in the post action, even though it's the item I'm editing.  Removing the attribute makes everything work as expected, so I think I'm misunderstanding the attribute's syntax.

Jul 25, 2012 at 5:40 AM

Oh.  Using SMinimum and SMaximum instead of dynamic works.  I didn't read the documentation very closely so I don't understand the difference.  But it works now.  I also ran across Scott Hanselman's blog post about just overriding the range method. :)  I think that's all I need anyway, since I was looking at the controls toolkit to solve this specific problem.  Still new to jquery, so I hadn't internalized that I could just do that.

Thanks for your replies, though!

Jul 25, 2012 at 8:25 AM

 Scott Hanselman's blog post works but doesnt furnish the code for setting automatically on the client side the same culture that is set on the server side. If you dont need the other functionalities of the mvc controls toolkit you can just use the override of the range operator + the code snippets I have in my globalization tutorial so you will have the client side cultutre set to the same server side culture. This is important, since as default autodetect the browser culture and set it automatically on the server. This way your web site will support automatically the culture set in any browser both on the cilent and the server side.

Anyway the Mvc Controls Toolkit support also a nice TextBox that formats automatically its content according to your data annotations. This might be useful to your application, since the automatic formatting is done according the th culture selected. Give a look: