电子邮件地址输入验证 [英] Email address input validation

查看:45
本文介绍了电子邮件地址输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 wpf C# 中对电子邮件地址进行文本框输入验证?正则表达式或验证表达式或任何可以提供帮助的东西,最好附上代码示例和一些说明

Is there any way to make a textbox input validation for email addresses in wpf C#? Regex or validation expression or anything that can help, best with code sample and some instructions

推荐答案

在 text_changed 事件中,您可以将文本框的值传递给辅助类.

On the text_changed event you could pass the value of the textbox to a helper class.

public static class ValidatorExtensions
{
    public static bool IsValidEmailAddress(this string s)
    {
        Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}

现在在文本更改事件上您可以测试输入

Now on the text changed event you can test the input

private void myTextBox_TextChanged(object sender, EventArgs e)
{
   bool result = ValidatorExtensions.IsValidEmailAddress( myTextBox.Text );
}

这篇关于电子邮件地址输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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