2009-07-17 5 views

답변

68

캐나다 우편 번호 및 로 시작할 수 없습니다 W 나 Z :

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

당신은 중간에 선택적인 공간하려면 :

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

+1 "캐나다 우편 번호는 특정 문자를 가질 수 없습니다". 첫 번째 글자가 더 제한되어 있고 두 번째와 세 번째 글자가 더 제한적이라고 덧붙이고 싶을 수도 있습니다. –

+0

답에 대한 톤 감사합니다 ...이 M4B1E8 같은 코드를 잘 작동하지만 ... M4B 1E8 작동하지 않습니다. 캐나다 우편 번호는 3 자 뒤의 공백을 가질 수 있습니다. 참조 : http : //www.mongabay.com/igapo/toronto_zip_codes.htm – Jimmy

+6

이상적으로 입력시 공백을 무시하고 데이터를 표준 형식으로 정규화하여 저장해야합니다. 그렇게하면 사람들은 공백을 넣거나 갖지 않고 우편 번호를 입력 할 수 있으며 중요하지 않습니다. 필요한 경우 출력을 위해 형식을 지정할 수 있습니다. – Rob

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

나는 HTTP에 따라, 첫 번째 문자에 유효하지 않은 문자를 제외하는 내 대답을 수정 한 ://www.infinitegravity.ca/postalcodeformat.htm. – Templar

+3

f 모든 슬래시에 대해 두 개의 슬래시를 사용하지 않으려면'@ "\ A [ABCEGHJKLMNPRSTVXY] \ d [A-Z]? \ d [A-Z] \ d \ z"'와 같이 @ 문자열 리터럴을 사용하십시오. – cdmckay

0

뭔가 : 문자 D, F, I, O, Q, 또는 U를 포함 할 수 없습니다

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

모든 문자가 우편 번호로 유효한 것은 아닙니다. – Tilendor

0

의미의 유효성을 검사하지 않고 우편 번호의 형식을 확인하는 것은 가치가 없습니다. 오타가 잘못된 주소에 대해 유효한 우편 번호를 여전히 가져올 수 있기 때문에 가능합니다. 주소 데이터베이스에 대해 코드의 유효성을 검사하려고합니다. 여기를 참조하십시오 http://www.canadapost.ca/cpo/mc/business/productsservices/atoz/postalcodeproducts.jsf

+0

캐나다 게시 링크가 죽었습니다 – billynoah

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

OP가 원합니다 정규식을 사용합니다. 이 대답에는 아무 것도 없습니다. –