如何验证邮政编码输入数据



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

     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。 - 根据下表,第一个数字必须与状态匹配:

状态 : NT|新南威尔士州|维克|QLD|SA|WA|塔斯|

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

看看正

则表达式:http://msdn.microsoft.com/fr-fr/library/system.text.regularexpressions.regex.aspx 这就是你需要的:)

正则表达式可用于验证邮政编码:

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

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

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

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
}

编辑:基于用户评论:

这是您可以使用的:

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

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

使用正则表达式:

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

最新更新