【问题标题】:How to validate post code input data?如何验证邮政编码输入数据?
【发布时间】:2012-08-10 09:42:55
【问题描述】:

这就是我到目前为止所做的

     Console.Write("Enter your post code >");
        post_code = Console.ReadLine();
        if (string.IsNullOrEmpty(post_code))
        {
            Console.WriteLine("Please enter correct data");
            return;
        }

        else if (post_code.Length != 4)
        {
            Console.WriteLine("Please enter correct data");
            return;
        }

我需要:-第一个数字不能是 1、8 或 9。 - 第一个数字必须与下表中的状态匹配:

州:新台币|新南威尔士州|维克|昆士兰|南非|西澳|塔斯|

第一个数字:0 | 2 | 3 | 4 | 5 | 6 | 7 |

【问题讨论】:

    标签: c# validation


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    Regular expressions可用于验证邮政编码:

        Regex postCodeValidation = new Regex(@"^[0234567]\d{4}$");
        if (postCodeValidation.Match(post_code).Success)
        {
            // Post code is valid
        }
        else
        {
            // Post code is invlid
        }
    

    请注意:在上面的代码中,邮政编码被认为是 5 位数字(要更改长度 - 您需要在 正则表达式模式中替换 4 @987654325 @ 带有适当的数字)。

    【讨论】:

    • 它不会编译("[0234567]\\d{4}"@"[0234567]\d{4}"),由于缺少字符串分隔符,它还会验证无效输入,例如 abc23456
    • @GeorgeTsoukatos 然后验证代码长度并使用 for 循环遍历邮政编码的所有字符并验证 IsDigit 方法(另外验证第一个字符)。
    • 我只需要使用 : if, if ... else, if ... else if ... else 结构  嵌套 ifs CASE 和 switch 结构
    • @GeorgeTsoukatos 看起来您想分别验证邮政编码的每个字符 - 那么最好的选择是使用 switch 运算符,其中所有数字的情况都连接起来(共享相同的代码块)并调用每个字符的函数内。
    • @GeorgeTsoukatos 请参考 Konrad Morawski 答案的第三版修订版 - 他提供的解决方案仅使用了 if(您可以通过单击打开它已编辑 [前段时间] 链接在他的答案底部)。
    【解决方案3】:

    我不确定状态是否是您的四字符邮政编码的一部分也包含状态。但如果没有,你可以这样做:

    Dictionary<string,string> statePostCodeMap = new Dictionary<string,string>();
    
    // Populate the dictionary with state codes as key and post code first character as value
    
    if(!post_code.StartsWith(statePostCodeMap([NameOfVariableThatHoldsState])){
    // Incorrect post code
    }
    

    编辑:基于用户 cmets:

    这就是你可以使用的:

    if !((string.Compare(state, "NT", true) == 0 && post_code.StartsWith("0"))){
    // Incorrect Data
    }
    else if(<similar condition for other values>)
    ...
    

    我认为这是某种学习练习。

    【讨论】:

    • 我只需要使用 : if, if ... else, if ... else if ... else 结构  嵌套 ifs CASE 和 switch 结构
    【解决方案4】:

    使用正则表达式:

    using System;
    using System.Text.RegularExpressions;
    
    namespace PostCodeValidator
    {
        class Program
        {
            static void Main(string[] args)
            {
                var regex = new Regex(@"^[0234567]{1}\d{3}$");
                var input = String.Empty;
    
                while (input != "exit")
                {
                    input = Console.ReadLine();
                    Console.WriteLine(regex.IsMatch(input));
                }
            }
        }
    }
    

    非正则表达式解决方案:

        static bool ValidPostCode(string code)
        {
            if (code == null || code.Length != 4)
            {
                return false;
            }
            var characters = code.ToCharArray();
            if (characters.Any(character => !Char.IsNumber(character)))
            {
                return false;
            }
            if ("189".Contains(characters.First()))
            {
                return false;
            }
            return true;
        }
    

    另一个,没有 LINQ:

        static bool SimpleValidPostCode(string code)
        {
            if (code == null || code.Length != 4)
            {
                return false;
            }
            if ("189".Contains(code[0]))
            {
                return false;
            }
            for (var i = 1; i < 4; i++)
            {
                if (!"123456789".Contains(code[i]))
                {
                    return false;
                }
            }
            return true;
        }
    

    I only can do it with : if, if … else, if … else if … else constructs Nested ifs CASE and switch constructs

    如果for 循环也不在您允许的语言结构列表中,您仍然可以尝试:

        static bool SimpleValidPostCode(string code)
        {
            if (code == null || code.Length != 4)
            {
                return false;
            }
            if (code[0] == '1') return false;
            if (code[0] == '8') return false;
            if (code[0] == '9') return false;
    
            return "0123456789".Contains(code[1]) && "0123456789".Contains(code[2]) && "0123456789".Contains(code[3]);            
        }
    

    【讨论】:

    • 我只能用 : if, if ... else, if ... else if ... else 结构  嵌套 ifs CASE 和 switch 结构来做到这一点
    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2013-10-09
    • 2018-07-21
    相关资源
    最近更新 更多