如何在文本框中验证电子邮件格式?

时间:2012-06-25 13:54:42

标签: windows-phone-7

我想放置验证器,以便如果用户没有以正确的格式(即abc@xyz.com)放置电子邮件ID,则会弹出错误消息。如何为Windows Mobile安装此验证器?

1 个答案:

答案 0 :(得分:2)

using System;
using System.Text.RegularExpressions;

  public static bool IsValidEmail(string strIn)
       {
           // Return true if strIn is in valid e-mail format.
           return Regex.IsMatch(strIn, 
                  @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + 
                  @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"); 
       }