2009-07-17 25 views

Respuesta

68

códigos postales canadienses no pueden contener las letras D, F, I, O, Q, o U, y no puede comenzar con W o Z:

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

Si desea un espacio opcional en el medio:

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

+1 para "Los códigos postales canadienses no pueden tener ciertas letras". Es posible que también desee agregar que la letra inicial es más restringida, luego la segunda y tercera. –

+0

Muchísimas gracias por la respuesta ... Esto funciona bien para los códigos como M4B1E8 ... pero no lo puedo trabajar para M4B 1E8. Canadá postal podría tener espacio después de 3 characters..reference aquí: http: //www.mongabay.com/igapo/toronto_zip_codes.htm – Jimmy

+6

Idealmente, usted debe ignorar el espacio en blanco en la entrada y normalizar los datos a un formato canónico para su almacenamiento. De esta forma, las personas pueden ingresar códigos postales con o sin espacios, y no importará. Puede formatearlos para fines de salida si es necesario. – Rob

2

Sugiero lo siguiente:

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

He modificado mi respuesta para excluir las cartas no válidos en el primer carácter, como por http://www.infinitegravity.ca/postalcodeformat.htm. – Templar

+3

f usted no quiere tener que hacer dos barras para cada raya vertical, utilice una cadena @ literal como en '@ "\ A [ABCEGHJKLMNPRSTVXY] \ d [A-Z]? \ D [A-Z] \ d \ z" '. – cdmckay

0

Algo como esto:

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

No todas las letras son válidas en los códigos postales. – Tilendor

1

Estas son las reglas 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

Prueba esto:

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

el PO quiere para usar expresiones regulares No hay ninguno en esta respuesta. –

Cuestiones relacionadas