邮箱及密码的正则表达式验证(C#)

      

       刚接触正则表达式,发现其功能竟然如此的“很好很强大”,于是用C#写了一个邮箱的验证,小弟菜菜,如有不足之处敬请指出,同时也希望对刚接触正则表达式的兄弟姐妹有所帮助!

       下面是代码部分:

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Text.RegularExpressions;



namespace RegulatorTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void btnTest_Click(object sender, EventArgs e)

        {

            //验证 E-mail 格式

            string regexEmail = "//w{1,}@//w{1,}//.//w{1,}";

            System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace 

                | System.Text.RegularExpressions.RegexOptions.Multiline)

                        | System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            System.Text.RegularExpressions.Regex regEmail = new System.Text.RegularExpressions.Regex(regexEmail, options);

            string email = txtEmail.Text;

            

            if (regEmail.IsMatch(email))//email 填写符合正则表达式 "//w{1,}@//w{1,}//.//w{1,}"

            {

                MessageBox.Show("符合正则表达式:"+regEmail.ToString()+"/n邮箱填写成功!");

            }

            else {

                MessageBox.Show("不符合正则表达式:"+regEmail.ToString()+"/n邮箱格式不正确!");

                return;

            }



            //验证密码 由不小于6位不大于15位的字母数字下划线特殊符号组成!

            string regexPwd = "^.{6,15}___FCKpd___0quot;;//限定开头,须从第一位开始匹配,限定结尾,总位数不得超过15位,否则即使大于15位仍然可以验证通过

            System.Text.RegularExpressions.Regex regPwd = new System.Text.RegularExpressions.Regex(regexPwd, options);

            string pwd = txtPwd.Text;

            if (regPwd.IsMatch(pwd))

            {

                MessageBox.Show("符合正则表达式:"+regPwd.ToString()+"/n密码验证通过!");

            }

            else {

                MessageBox.Show("不符合正则表达式:"+regPwd.ToString()+"/n密码验证失败!密码应由不小于6位不大于15位的字母数字下划线特殊符号组成!");

                return;

            }

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }

    }

}

你可能感兴趣的:(Visual,Studio)