How to install client side validation scripts
Steps to install cldr-data files that are required for client side validation of localized values as decimal numbers and date
Validating decimal numbers, dates and currency requires installing cldr and cldr-data scripts locally as below:
- in the projects root creat a new json file and name it libman.json the add the required libraries for localization as below:
{ "version": "1.0", "defaultProvider": "jsdelivr", "libraries": [ { "library": "cldrjs@0.5.1", "destination": "wwwroot/lib/cldr" }, { "library": "cldr-data@35.1.0", "destination": "wwwroot/lib/cldr-data" }, { "library": "globalize@1.4.2", "destination": "wwwroot/lib/globalize" } ] }
When you save the file, it will automatically install all libraires to wwwroot/lib folder. But we still need the localized culture based scripts (e.g.: numbers, dates, ...etc).
After intalling is done, open wwwroot/lib/cldr-data/package.json file and add w code e the last closing paranthes }.
"peerDependencies": { "cldr-data": ">=26" }
When you save the file it will download all localized systems for all cultures, this may take a while depending on your internet speed.
How to use all these scripts:
- Auto option :
See LocalizationValidationScripts TagHelper for details.
- Manual option :
create a new partial view under Pages/Shared folder and name it : _LocalizationValidationScriptsPartial.cshtml and add nsert below code :
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment @{ string GetDefaultLocale() { const string localePattern = "lib\\cldr-data\\main\\{0}"; var currentCulture = System.Globalization.CultureInfo.CurrentCulture; var cultureToUse = "en"; //Default regionalisation to use if (System.IO.Directory.Exists(System.IO.Path.Combine(HostingEnvironment.WebRootPath, string.Format(localePattern, currentCulture.Name)))) cultureToUse = currentCulture.Name; else if (System.IO.Directory.Exists(System.IO.Path.Combine(HostingEnvironment.WebRootPath, string.Format(localePattern, currentCulture.TwoLetterISOLanguageName)))) cultureToUse = currentCulture.TwoLetterISOLanguageName; return cultureToUse; } }
Finally, use the partial after the default apps valdiation cripts partial view where you need to validiate localized input fields .