我需要使用C#(.NET)正則表達式驗證加拿大郵政編碼(例如,M4B 1C7
)。加拿大郵政編碼驗證
45
A
回答
68
加拿大郵政編碼不能包含字母d,F,I,O,Q,或U,和不能啓動W或Z:
[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
如果你想在中間的可選空間:
[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
2
我建議如下:
bool FoundMatch = false;
try {
FoundMatch = Regex.IsMatch(SubjectString, "\\A[ABCEGHJKLMNPRSTVXY]\\d[A-Z] ?\\d[A-Z]\\d\\z");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
0
0
驗證郵政編碼的格式而不驗證其含義是不值得的,因爲拼寫錯誤仍然會導致錯誤地址的有效郵政編碼。你想根據地址數據庫驗證代碼。見http://www.canadapost.ca/cpo/mc/business/productsservices/atoz/postalcodeproducts.jsf
1
下面是規則http://en.wikipedia.org/wiki/Postal_code#Reserved_characters
ABCEGHJKLMNPRSTVXY <-- letter used
DFIOQU <-- letters not used because it mixes up the reader
WZ <-- letters used but not in the first letter
With that in mind the following in the proper regex
@[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][\s][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]
0
試試這個:
function postalCodeCheck (postalCode, type) {
if (!postalCode) {
return null;
}
postalCode = postalCode.toString().trim();
var us = new RegExp("^\\d{5}(-{0,1}\\d{4})?$");
// var ca = new RegExp(/^((?!.*[DFIOQU])[A-VXY][0-9][A-Z])|(?!.*[DFIOQU])[A-VXY][0-9][A-Z]\ ?[0-9][A-Z][0-9]$/i);
var ca = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]()?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i);
if(type == "us"){
if (us.test(postalCode.toString())) {
console.log(postalCode);
return postalCode;
}
}
if(type == "ca")
{
if (ca.test(postalCode.toString())) {
console.log(postalCode);
return postalCode;
}
}
return null;
}
-2
class Program
{
static void Main(string[] args)
{
string c1;
string c2;
string c3;
string c4;
string c5;
string c6;
string c7;
int sortie;
bool parfais = true;
Console.WriteLine("entrer votre code postal");
string cp = Console.ReadLine();
if (cp.Length == 7)
{
c1 = cp.Substring(0, 1);
c2 = cp.Substring(1, 1);
c3 = cp.Substring(2, 1);
c4 = cp.Substring(3, 1);
c5 = cp.Substring(4, 1);
c6 = cp.Substring(5, 1);
c7 = cp.Substring(6, 1);
if (int.TryParse(c1, out sortie))
{
parfais = false;
Console.WriteLine("le 1er caratere doit etre une lettre");
}
if (int.TryParse(c2, out sortie) == false)
{
parfais = false;
Console.WriteLine("le 2e caratere doit etre un nombre");
}
if (int.TryParse(c3, out sortie))
{
parfais = false;
Console.WriteLine("le 3e caratere doit etre une lettre");
}
if (c4.Contains(" ") == false)
{
parfais = false;
Console.WriteLine("vous devez utiliser un espace");
}
if (int.TryParse(c5, out sortie) == false)
{
parfais = false;
Console.WriteLine("le 5e caratere doit etre un nombre");
}
if (int.TryParse(c6, out sortie))
{
parfais = false;
Console.WriteLine("le 6e caratere doit etre une lettre");
}
if (int.TryParse(c7, out sortie) == false)
{
parfais = false;
Console.WriteLine("le 7e caratere doit etre un nombre");
}
else if(parfais == true)
{
Console.WriteLine("code postal accepter");
Console.ReadLine();
}
}
else
{
Console.WriteLine("le code postal doit contenir 7 caratere incluant l'espace");
}
Console.ReadLine();
相關問題
- 1. C# - 驗證美國或加拿大郵政編碼
- 2. 加拿大郵政'查找郵政編碼'屏幕刮碼
- 3. 驗證文本輸入匹配加拿大郵政編碼(例如「A1S2S3」)
- 4. Flex:禁用非美國或加拿大的郵政編碼驗證
- 5. 郵政編碼驗證
- 6. 驗證郵政編碼?
- 7. Postgresql - 域名爲加拿大郵政編碼
- 8. 問題與正則表達式爲加拿大郵政編碼
- 9. Oracle Spatial:針對加拿大郵政編碼的sdo_nn_distance問題
- 10. 從郵政編碼獲取街道名稱 - 加拿大
- 11. 用於郵政編碼驗證的Javascript
- 12. jquery驗證郵政編碼的規則
- 13. jQuery驗證郵政編碼和城市
- 14. JQuery驗證與郵政編碼
- 15. 郵政編碼半徑驗證
- 16. 驗證Access中的郵政編碼?
- 17. jQuery驗證英國郵政編碼
- 18. 在jQuery中驗證郵政編碼?
- 19. Acumatica郵政編碼驗證和匹配
- 20. 我如何驗證我的數組中的加拿大人(M3M 2B2)等郵政編碼?
- 21. 郵政編碼
- 22. 郵政編碼
- 23. 編寫JavaScript郵政編碼驗證功能
- 24. 如何驗證用戶輸入的郵政編碼是否正確美國郵政編碼
- 25. 如果郵政編碼格式動態變化,如何在java中驗證郵政編碼?
- 26. 郵政信箱驗證
- 27. 加拿大郵政AddressComplete「填充」不工作
- 28. 獲取加拿大郵政發貨警告(REST + PHP)
- 29. WP電子商務加拿大郵政發貨模塊
- 30. 如何在VB.NET中驗證郵政編碼?
+1「加拿大郵政編碼不能有某些字母」。你可能還想補充一點,領導信件更受限制,然後是第二和第三。 – 2009-07-18 04:03:07
謝謝你的回答...這對M4B1E8等代碼工作正常...但它不適用於M4B 1E8。加拿大郵政在3個字符之後可能有空格。參考:http://www.mongabay.com/igapo/toronto_zip_codes.htm – Jimmy 2009-07-18 21:57:33