2009-07-17 84 views

Trả lời

68

mã bưu chính Canada không thể chứa các chữ cái D, F, I, O, Q, hoặc U, và không thể bắt đầu với W hoặc Z:

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9] 

Nếu bạn muốn một không gian tùy chọn ở giữa:

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9] 
+4

+1 cho "mã bưu chính của Canada không thể có các chữ cái nhất định". Bạn cũng có thể muốn thêm rằng chữ cái hàng đầu bị hạn chế hơn, sau đó là chữ cái thứ hai và thứ ba. –

+0

cảm ơn một tấn cho câu trả lời ... điều này hoạt động tốt cho các mã như M4B1E8 ... nhưng nó sẽ không làm việc cho M4B 1E8. Bưu điện Canada có thể có không gian sau 3 ký tự..được đề cập ở đây: http: //www.mongabay.com/igapo/toronto_zip_codes.htm – Jimmy

+6

Lý tưởng nhất, bạn nên bỏ qua khoảng trống trên đầu vào và chuẩn hóa dữ liệu thành định dạng chuẩn để lưu trữ. Bằng cách đó, mọi người có thể nhập mã bưu điện có hoặc không có khoảng trắng và không thành vấn đề. Bạn có thể định dạng chúng cho mục đích đầu ra nếu cần. – Rob

2

Tôi đề nghị như sau:

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

Tôi đã sửa đổi câu trả lời của tôi để loại trừ chữ không hợp lệ trong ký tự đầu tiên, theo http://www.infinitegravity.ca/postalcodeformat.htm. – Templar

+3

f bạn không muốn phải thực hiện hai dấu gạch chéo cho mỗi dấu gạch chéo, sử dụng một chuỗi @ chữ như trong '@" \ A [ABCEGHJKLMNPRSTVXY] \ d [A-Z]? \ D [A-Z] \ d \ z "'. – cdmckay

0

Something như thế này:

^[A-Z]\d[A-Z] \d[A-Z]\d$ 
+1

Không phải tất cả các chữ đều hợp lệ trong mã bưu điện. – Tilendor

1

Dưới đây là các quy tắc 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

Hãy thử điều này:

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

OP muốn sử dụng cụm từ thông dụng. Không có câu trả lời nào trong câu trả lời này. –

Các vấn đề liên quan