如何在离开事件时验证C#中的文本框以检查文本框中的某些格式? [英] How do I validate textbox in C# to check certain format in textbox at leave event?

查看:146
本文介绍了如何在离开事件时验证C#中的文本框以检查文本框中的某些格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证Windows窗体中的文本框以接受特定格式的用户输入
我需要用户输入前405位和505位的前3位数字,然后是1950年至当前年份的年份,再加上6位唯一标识符.
我需要使用请假事件来验证文本框,以便验证用户输入是否与选项卡按钮单击,鼠标单击其他字段上的格式匹配.

文本框为13位数字.

私人void txtsfn_Leave(object sender,EventArgs e)
{
//这部分需要帮助
}

感谢您提供任何建议和帮助.

I am trying to validate a textbox in windows form to accept user input in certain format
I need user input as first 3 digits as number 405 & 505 followed by year ranging 1950-current year, followed by six digit unique identifier.

I need to validate textbox with leave event, such that it verifies user input matches the format on tab button click, mouse click to other field.

Textbox is 13 digit.

private void txtsfn_Leave(object sender,EventArgs e)
{
// need help with this part
}

Any suggestions and help is appreciated.

推荐答案

如果要使用MaskedTextBox:
If you want to use a MaskedTextBox:
// the character here should be whatever you set
// as the PromptChar property of the MaskedTextBox
private char[] splitchar = new char[1] {'-'};
private string[] fields;

private void resetMTB()
{
    // for debugging only
    Console.WriteLine("bad input: ", maskedTextBox1.Text);

    maskedTextBox1.Clear();
    maskedTextBox1.Focus();
    maskedTextBox1.Capture = true;
}

private void maskedTextBox1_Leave(object sender, EventArgs e)
{
    if(maskedTextBox1.Text.Length != 15)
    {
        resetMTB();
        return;
    }

    fields = maskedTextBox1.Text.Split(splitchar);

    int field1;

    if (fields.Length == 3)
    {
        if (fields[0].Length == 3
            && fields[1].Length == 4
            && fields[2].Length == 6)
        {
            if (Int32.TryParse(fields[1], out field1))
            {
                if (field1 >= 1950 && field1 <= DateTime.Now.Year) return;
            }
        }
    }

    resetMTB();
}

注意:该代码编写迅速,仅在很小的输入范围内进行了测试;在生产"代码中使用此代码之前,请确保进行彻底的测试.

Note: this code was written quickly, and tested with only a small range of inputs; be sure and test thoroughly before you use this in "production" code.


使用正则表达式非常容易.
要了解有关该信息的信息,可以使用本网站作为起点:正则表达式信息 [验证事件 [ ^ ]?它是为此目的而制成的.当焦点离开控件时会触发.

确保添加
This is pretty easy to do with a regular expression.
To learn about that you can use this site as a starting point: Regular Expression Info[^]

And why not use the Validating event[^] for the text box? It is kind of made for this purpose. And it will trigger when the focus leaves the control.

Make sure to add
using System.Text.RegularExpressions;


并将其添加为您的班级成员


also add this as a member of your class

private static Regex exprNumber = new Regex("^(405|505)(19[5-9][0-9]|[2-9][0-9]{3})[0-9]{6}


");
");


* The ^ anchor will make sure that you start the evaluation at the start of the string
* (405|505) means either 405 or 505
* 19[5-9][0-9] means 19 followed by a digit >=5 followed by 1 digit in the range of 0-9.
* 2[0-9]{3} means 2 - 9 followed by 3 digits in the range of 0-9. 2000 - 9999
* (19[5-9][0-9]|2[0-9]{3}) means either a number between 1950-1999 or between 2000-9999
* [0-9]{6} means exactly 6 digits in the range 0-9
* The


这篇关于如何在离开事件时验证C#中的文本框以检查文本框中的某些格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆