zoukankan      html  css  js  c++  java
  • 建立用户、密码类型

            每次做用户密码等类型的验证都很麻烦,最近想了个办法来简化判断的逻辑。

           .Net中提供了隐式转换,虽然降低了代码可读性,但是使用确实很方便。而且我认为,定义自己的类型使用它并不会降低可读性。

           可以定义用户类型UserName
     1 using System;
     2 using System.Text.RegularExpressions;
     3 
     4 namespace SiteModel.Business
     5 {
     6     /// <summary>
     7     /// UserName 的摘要说明。
     8     /// </summary>
     9     public class UserName
    10     {
    11         private string username;
    12 
    13         public UserName(string username)
    14         {
    15             this.username = username;
    16         }
    17 
    18         public static implicit operator UserName(string password) {
    19             if(!AccessName(password))         //如果用户名不能通过正则表达式验证,则返回null值
    20                 return null;
    21             return new UserName(password.Trim());      //返回UserName实例对象
    22         }
    23 
    24         private static bool AccessName(string name) {
    25             Regex reg = new Regex("^[a-zA-Z][a-zA-Z0-9]{4,15}$");
    26             if(reg.Match(name).Success)
    27                 return true;
    28             return false;
    29         }
    30     }
    31 }
    32 
    使用的时候
    SiteModel.Business.UserName username = name.Text;   //name为TextBox控件
       if(username==null) {
              Label1.Text = "<br>用户名格式输入错误!";
              return;
       }
    这就简单了。


    密码类型可以这样定义
     1 using System;
     2 using System.Text.RegularExpressions;
     3 
     4 namespace SiteModel.Business
     5 {
     6     /// <summary>
     7     /// PassWord 的摘要说明。
     8     /// </summary>
     9     public class PassWord
    10     {
    11         private string password;
    12 
    13         public PassWord(string password)
    14         {
    15             this.password = password;
    16         }
    17 
    18         public static implicit operator PassWord(string password) {

    20             if(!AccessPass(password))      //如果不通过正则表达式验证,则返回null
    21                 return null;
    22             return new PassWord(password.Trim().ToLower()).MD5();   //返回实例后MD5加密(转换成小写,不区分大小写)
    23         }
    24 
    25         private string MD5() {
    26             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.ToString(),"MD5").ToLower(); 
    27         }
    28 
    29         private static bool AccessPass(string pass) {
    30             Regex reg = new Regex("^[a-zA-Z0-9]{6,16}$");
    31             if(reg.Match(pass).Success)
    32                 return true;
    33             return false;
    34         }
    35     }
    36 }
    37 
    使用也是一样
    SiteModel.Business.PassWord password = pass.Value;
       if(password==null) {
              Label2.Text = "<br>密码格式输入错误!";
             return;
       }

    照这样的方法也能定义出其他项目需要的特殊数据类型。

    谢平   2006年8月29日
    http://www.cnblogs.com/birdshover/
  • 相关阅读:
    BEC listen and translation exercise 44
    中译英12
    BEC listen and translation exercise 43
    中译英11
    BEC listen and translation exercise 42
    中译英10
    BEC listen and translation exercise 41
    中译英9
    BEC listen and translation exercise 40
    中译英8
  • 原文地址:https://www.cnblogs.com/birdshover/p/a1.html
Copyright © 2011-2022 走看看