验证电子邮件地址Valider的类

    技术2022-05-11  53

    // 验证电子邮件地址 using  System; namespace  Valider{     ///   <summary>      ///  Valider 的摘要说明。     ///   </summary>          // 定义开始验证事件委托      public   delegate   void  VeginValidData();     // 定义结束验证时间委托      public   delegate   void  EndValidData();     public   class  Valider    {         // 电子邮件是否合法,合法为true,不合法为false          private   bool  accept;         // 要验证的电子邮件地址          private   string  vldemail;                 // 开始验证事件          public   event  VeginValidData onBeginValid;         // 结束验证事件          public   event  EndValidData onEndValid;                 // 电子邮件地址是否合法          public   bool  Accept        {             get             {                 return   this .accept;            }        }         ///   <summary>          ///  要验证的电子邮件地址         ///   </summary>          public   string  Vldemail        {             get             {                 return   this .vldemail;            }             set             {                 this .vldemail  =  value;            }        }         ///   <summary>          ///  构造函数         ///   </summary>          public  Valider()        {             this .onBeginValid  +=   new  VeginValidData(Valider_onBeginValid);             this .onEndValid  +=   new  EndValidData(Valider_onEndValid);             this .accept  =   false ;             this .vldemail  =   "" ;        }                 ///   <summary>          ///  公共验证Email方法         ///   </summary>          public   void  proccess()        {             // 调用开始验证事件              this .onBeginValid();             // 验证@字符              this .validAtSym();             if  ( accept )            {                 // 验证用户名                  this .validUserName();            }             // 调用结束验证事件              this .onEndValid();        }         ///   <summary>          ///  电子邮件信息的@字符验证         ///   </summary>          private   void  validAtSym()        {             // 从前取@所在的位置              int  intBeginAtSymIndex  =   this .vldemail.IndexOf( " @ " );                 // 从后取@所在的位置              int  intEndAtSymIndex  =   this .vldemail.LastIndexOf( " @ " );             // 是否有@              if  ( intBeginAtSymIndex  ==   - 1  )            {                 this .accept  =   false ;                 return ;            }             // 是否有两个@              if  ( intBeginAtSymIndex  !=  intEndAtSymIndex )            {                 this .accept  =   false ;                 return ;            }             // @是否在中间              if  ( intBeginAtSymIndex  ==   0   ||  intBeginAtSymIndex  ==  ( this .vldemail.Length - 1 ) )            {                 this .accept  =   false ;                 return ;            }             this .accept  =   true ;        }                 ///   <summary>          ///  对电子邮件信息的用户名验证         ///   </summary>          private   void  validUserName()        {             // 取逗号所在的位置              int  intCommaIndex  =   this .vldemail.IndexOf( " , " );             // 是否有逗号              if  ( intCommaIndex  >   - 1  )            {                 this .accept  =   false ;                 return ;            }             // 取@所在的位置              int  intAtSymIndex  =   this .vldemail.IndexOf( " @ " );                 // 取用户名              string  strUserName  =   this .vldemail.Substring( 0 , this .vldemail.Length  -  intAtSymIndex - 1 );            strUserName = this .vldemail.Substring( 0 ,intAtSymIndex);             // 用户名是否有3位              if  ( strUserName.Length  <   3  )            {                 this .accept  =   false ;                 return ;            }                         // 取第一个字符              string  strFirst  =  strUserName.Substring( 0 , 1 );             // 第一个字符是否为数字              try             {                 int  intFirst  =  Convert.ToInt32(strFirst);                 this .accept  =   false ;                 return ;            }             catch (Exception)            {                 this .accept  =   true ;            }             this .accept  =   true ;        }         // 结束验证          private   void  Valider_onEndValid()        {            Console.WriteLine( " 验证结束! " );        }                 // 开始验证          private   void  Valider_onBeginValid()        {            Console.WriteLine( " 开始验证! " );        }    }}

     下面是一个测试类

     

    using  System; namespace  Valider{     ///   <summary>      ///  Class1 的摘要说明。     ///   </summary>      class  Email    {         ///   <summary>          ///  应用程序的主入口点。         ///   </summary>         [STAThread]         static   void  Main( string [] args)        {            Console.WriteLine( " 请输入您的电子邮件: " );             // 实例化Valider             Valider valider  =   new  Valider();             // 输入电子邮件地址             valider.Vldemail  =  Console.ReadLine();             // 验证电子邮件             valider.proccess();             // 判断是否合法              if  ( valider.Accept )            {                Console.WriteLine( " 您的电子邮件合法,系统可以接受! " );            }             else             {                Console.WriteLine( " 您的电子邮件不合法,系统不可以接受! " );            }            Console.ReadLine();                }    }}

    最新回复(0)