Skip to content

Instantly share code, notes, and snippets.

@johnnyreilly
Last active October 10, 2015 06:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnnyreilly/3651751 to your computer and use it in GitHub Desktop.
Save johnnyreilly/3651751 to your computer and use it in GitHub Desktop.
A monkey patch for jquery.validate.js that allows validation to be internationalised
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.IO;
using System.Globalization;
namespace My.Helpers
{
/// <summary>
/// Static class that is a store for commonly used filenames
/// (so if the files are updated they only need to be amended in a single place)
/// </summary>
public static class GlobalizeUrls
{
/// <summary>
/// URL for Globalize: https://github.com/jquery/globalize
/// </summary>
public static string Globalize { get { return "~/Scripts/globalize.js"; } }
/// <summary>
/// URL for the specific Globalize culture
/// </summary>
public static string GlobalizeCulture
{
get
{
//Determine culture - GUI culture for preference, user selected culture as fallback
var currentCulture = CultureInfo.CurrentCulture;
var filePattern = "~/scripts/globalize/globalize.culture.{0}.js";
var regionalisedFileToUse = string.Format(filePattern, "en-GB"); //Default localisation to use
//Try to pick a more appropriate regionalisation
if (File.Exists(HostingEnvironment.MapPath(string.Format(filePattern, currentCulture.Name)))) //First try for a globalize.culture.en-GB.js style file
regionalisedFileToUse = string.Format(filePattern, currentCulture.Name);
else if (File.Exists(HostingEnvironment.MapPath(string.Format(filePattern, currentCulture.TwoLetterISOLanguageName)))) //That failed; now try for a globalize.culture.en.js style file
regionalisedFileToUse = string.Format(filePattern, currentCulture.TwoLetterISOLanguageName);
return regionalisedFileToUse;
}
}
}
}
(function ($, Globalize) {
// Clone original methods we want to call into
var originalMethods = {
min: $.validator.methods.min,
max: $.validator.methods.max,
range: $.validator.methods.range
};
// Tell the validator that we want numbers parsed using Globalize
$.validator.methods.number = function (value, element) {
var val = Globalize.parseFloat(value);
return this.optional(element) || ($.isNumeric(val));
};
// Tell the validator that we want dates parsed using Globalize
$.validator.methods.date = function (value, element) {
var val = Globalize.parseDate(value);
return this.optional(element) || (val);
};
// Tell the validator that we want numbers parsed using Globalize,
// then call into original implementation with parsed value
$.validator.methods.min = function (value, element, param) {
var val = Globalize.parseFloat(value);
return originalMethods.min.call(this, val, element, param);
};
$.validator.methods.max = function (value, element, param) {
var val = Globalize.parseFloat(value);
return originalMethods.max.call(this, val, element, param);
};
$.validator.methods.range = function (value, element, param) {
var val = Globalize.parseFloat(value);
return originalMethods.range.call(this, val, element, param);
};
}(jQuery, Globalize));
$(document).ready(function () {
// Set Globalize to the current culture driven by the html lang property
var currentCulture = $("html").prop("lang");
if (currentCulture) {
Globalize.culture(currentCulture);
}
});
@quebecmabe
Copy link

You must add !isNaN to your condition if you want the "0" value to be valid.

Replace

if (Globalize.parseFloat(value))

By

if (!isNaN(Globalize.parseFloat(value)))

@johnnyreilly
Copy link
Author

Thanks @quebecmabe - I switched to using isNumeric in the end for clarity's sake. Good spot with the false-y values though!

@psavva
Copy link

psavva commented Jul 10, 2014

Hi Johnny,

I've been trying to implement your solution. I'm trying to have a fully localized application, but coming into problems with jquery validate.

In any case, i'm getting this error:
jquery.validate.globalize.js:42
Uncaught ReferenceError: Globalize is not defined

I've ensured that i'm loading all prerequisites correctly, and still coming into this error.
Any suggestions?

@Auronmatrix
Copy link

Nice, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment