i18n Validations

和自甴很熟 提交于 2020-01-12 01:38:11

问题


Think Global, Act Local

That's what they tell you, however during all my time as I developer I've always seen big companies like Google, Microsoft, Oracle and so do validations in a localized manner: they know which country I'm from so they will try to validate my phone number, postal code and other details such as bank account numbers with the proper validation methods for my country. Yesterday I signed up for Google checkout and they even showed me some examples of postal code formats in my country!

So my question is, how do they do this? I know that they have thousands of employees but I find it hard to believe that they all had to reinvent the well. There are countless validations methods for the US but what about the rest of the world? I've not yet seen a single open source decent library (apart from a very incomplete and outdated PEAR attempt) to perform validations on various common aspects of countries such as:

  • Civil ID
  • Tax ID
  • SSN (Social Security Number)
  • BBAN (Basic Bank Account Number)
  • Fax, Phone and Mobile Numbers
  • Postal / Zip Codes
  • License Plates
  • Banknote Serial Numbers
  • and so on...

Is there any well hidden resource that I'm unaware of?


回答1:


There's a huge difference between simple and full validation. It is often impossible to standardize validation fully across locales, here are a couple of examples:

  • CivilID/SSN etc. numbers can allow/disallow substitute values (system specific), foreign values, historic values etc. We have 2 different validation systems in one of our applications for the same field, depending on context, and other IT systems may have other validation systems.

  • ZIP code validation may require access to a list of valid values, which is something you need to subscribe to. In many countries, this subscription is not automated, and because of copyright rules, it is not trivial to redistribute.

  • Phone numbers have a similar problem. Do you allow expensive numbers, mobile phone numbers? How do you detect them?

You may argue that a simple validation is possible in most cases - but for simple validation, it is often easier to use pattern matching and to let the local department in each market define the valid patterns.

There is also a problem with the software service contract. If you buy software and want to use it for 5 years, then it is vital that you can change the validation rules, if something changes. I have experienced 4 changes of the phone number system in my country that affected simple validation like "number of digits" and "what can the first digit be".

Therefore, the best solution is to have a centralized database of locale-specific patterns, and to allow the administrators to modify the validation patterns.




回答2:


The PEAR one is not too bad. For example, the validator for New Zealand has all kinds of useful stuff like postal codes, IRD numbers, telephone numbers, bank account numbers http://pear.php.net/manual/en/package.validate.validate-nz.php

Its still in Alpha, but covers quite a few countries. Might be worth reconsidering?




回答3:


Wikipedia collects license plate information, by country. Likewise for postal codes.




回答4:


Well , i think all it takes is one programmer from that country and a couple of days for reasearch & write the validations .. nothing special .. + there is tons of info in wikipedia




回答5:


Perhaps from http://www.geonames.org/ they have a wonderful api too



来源:https://stackoverflow.com/questions/1384762/i18n-validations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!