C#使用正则表达式验证E-Mail格式
1
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8
//using System.Text.RegularExpressions;9
namespace Ex06_0110


{11
public partial class Form1 : Form12

{13
public Form1()14

{15
InitializeComponent();16
}17
18
private void button1_Click(object sender, EventArgs e)19

{20
if (!IsEmail(textBox1.Text)) 21
MessageBox.Show("Email格式不合格"); 22
else 23
MessageBox.Show("Email格式合格"); 24
}25
public bool IsEmail(string str_Email)26

{27
return System.Text.RegularExpressions.Regex.IsMatch(str_Email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");28
}29
}30
}

浙公网安备 33010602011771号