C# 加拿大邮政编码验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1146202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:19:36  来源:igfitidea点击:

Canadian postal code validation

c#.netregex

提问by Jimmy

I need to validate a Canadian postal code (for example, M4B 1C7) using C# (.NET) regular expressions.

我需要M4B 1C7使用 C# (.NET) 正则表达式验证加拿大邮政编码(例如,)。

采纳答案by Gordon Gustafson

Canadian postal codes can't contain the letters D, F, I, O, Q, or U, and cannot start with W or Z:

加拿大邮政编码不能包含字母 D、F、I、O、Q 或 U,并且不能以 W 或 Z 开头:

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

If you want an optional space in the middle:

如果你想在中间有一个可选的空间:

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

回答by Templar

I suggest the following:

我建议如下:

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
}

回答by Colin Mackay

Something like this:

像这样的东西:

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

回答by joe snyder

Validating the format of a postal code without validating its meaning isn't worth it, since typos can still result in a valid postal code for the wrong address. You want to validate the code against the address database. See http://www.canadapost.ca/cpo/mc/business/productsservices/atoz/postalcodeproducts.jsf

在不验证其含义的情况下验证邮政编码的格式是不值得的,因为拼写错误仍然可能导致错误地址的有效邮政编码。您想根据地址数据库验证代码。见http://www.canadapost.ca/cpo/mc/business/productsservices/atoz/postalcodeproducts.jsf

回答by user3111634

Here are the rules http://en.wikipedia.org/wiki/Postal_code#Reserved_characters

以下是规则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]

回答by mmadush

Try this:

尝试这个:

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;
}

回答by guillaume

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();