Posted By

SnipplrSlush on 07/03/13


Tagged

javascript jquery aspnet validate MVC 4


Versions (?)

Globalize jQuery Validate for ASP.Net MVC 4


 / Published in: JavaScript
 

jQuery Validate is used in ASP.Net MVC 4. As it is checking only en-US as a default you may have problems with different cultures for decimal inputs and dates (I had to try for minutes to find a accepted date format. I don't want to see how my customers would react). The decimal has been converted from the accepted 9.90 to 990,00 by the controller.

  1. // you need globalize.js and globalize.culture.de-DE.js for this example.
  2. // this overrides the default methods of jQuery Validate so load it only if you want to
  3. // use german globalization.
  4.  
  5. $.validator.methods.number = function (value, element) {
  6. return this.optional(element) || !isNaN(Globalize.parseFloat(value));
  7. };
  8.  
  9. $.validator.methods.date = function (value, element) {
  10. var check = false;
  11. var re = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
  12. if (re.test(value)) {
  13. var adata = value.split('.');
  14. var dd = parseInt(adata[0], 10);
  15. var mm = parseInt(adata[1], 10);
  16. var yyyy = parseInt(adata[2], 10);
  17. var xdata = new Date(yyyy, mm - 1, dd);
  18. if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
  19. check = true;
  20. else
  21. check = false;
  22. } else
  23. check = false;
  24. return this.optional(element) || check;
  25. };
  26.  
  27. $(function () {
  28. Globalize.culture('de-DE');
  29. });

Report this snippet  

You need to login to post a comment.