简体   繁体   English

如何检查邮政编码是否在C#文本框中正确输入

[英]How to check if a postal code is entered correctly in a textbox C#

For my school I need to make a program that checks if you give in the right postal code: for example it need to be in this format: 0000 XX. 对于我的学校,我需要编写一个程序来检查您输入的邮政编码是否正确:例如,它必须采用以下格式:0000 XX。 It's a dutch postal code. 这是荷兰的邮政编码。

I'm trying to find out how to do this, but I really don't know how to fix this problem. 我正在尝试找出解决方法,但我真的不知道该如何解决。 i can't use Regex, because we havent learnt that yet. 我不能使用正则表达式,因为我们还没有学到。

The events that I'm allowed to use: 我被允许使用的事件:

TextChanged
KeyDown
KeyPress
KeyUp

Functions that i'm allowed to use: 我被允许使用的功能:

char.isNumber(); char.isNumber();
char.isLetter(); char.isLetter();

If anyone could help me, that would be awesome. 如果有人可以帮助我,那将很棒。 Thanks in advance. 提前致谢。

As a help for a homework: 作为作业的帮助:

In the OnTextChanged-event iterate through the String: 在OnTextChanged事件中,对字符串进行迭代:

if string is longer than 7?: fail

for each character in string:
  if char-pos <= 4 && character is not number: fail
  if char-pos == 5 && character is not whitespace: fail
  if char-pos > 5 && character is not letter: fail
end

good luck. 祝好运。

With respect, you CAN use RegEx - because RegEx is the proper solution that we would all use. 相对而言,您可以使用RegEx-因为RegEx是我们都会使用的正确解决方案。 Don't be afraid to move forward from your course and learn things on your own as well... 不要害怕走出自己的道路,也可以自己学习东西。

However, sticking to the functions you highlight... 但是,坚持使用突出显示的功能...

using System;

namespace SO5
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Console.WriteLine(TestPostcode("1234 XC"));
            Console.WriteLine(TestPostcode("D4 XC"));
            Console.WriteLine(TestPostcode("4632 XC1"));

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }

        public static bool TestPostcode(string input)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (char c in input)
            {
                if (char.IsLetter(c))
                {
                    sb.Append("L");
                }
                else if (char.IsNumber(c))
                {
                    sb.Append("N");
                }
            }

            return sb.ToString() == "NNNNLL";

        }

    }
}

Now, if you want to impress, read about something called Unit Testing and a (free) product called NUnit. 现在,如果您想留下深刻的印象,请阅读有关单元测试的知识和有关NUnit的(免费)产品的信息。 You will then be able to both execute your method with test cases at will, whilst also having effective tests that the thing still works... 这样,您就可以随意使用测试用例执行您的方法,同时还可以进行有效的测试以证明事情仍然可行...

A few ways you can go about it, but i'll give you a hint to how I would do it. 您可以通过几种方法进行操作,但是我会给您一些提示,告诉我如何操作。

Handle TextChanged, and in that method get the string for the text entered. 处理TextChanged,然后在该方法中获取输入文本的字符串。

Loop through each character, and depending on the position, check for the type of character you are looking for. 循环浏览每个字符,然后根据位置检查要查找的字符类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM