检测函数集合(1)

继续记录没有技术含量的函数,这次的比较有用。

using System.Text.RegularExpressions;

namespace QS_Web
{
    
namespace Kobold
    
{
        
public class Censor
        
{
            
public static bool CheckMode1(string strInput,int intMin,int intMax)//由a~z的英文字母(不区分大小写)、0~9的数字、点或下划线组成,长度为intMin~inMax个字符
            {
                
if(intMin <= intMax)
                
{
                    
try
                    
{
                        
return Regex.IsMatch(strInput,@"^[a-zA-Z0-9._]{" + intMin + "," + intMax + "}$");
                    }

                    
catch
                    
{
                        
return false;
                    }

                }

                
else
                
{
                    
return false;
                }

            }

        
            
public static bool CheckNumber(string strInput,int intMin,int intMax)//是否是指定长度的数字
            {
                
if(intMin <= intMax)
                
{
                    
try
                    
{
                        
return Regex.IsMatch(strInput, @"^\d{" + intMin + "," + intMax + "}$");
                    }

                    
catch
                    
{
                        
return false;
                    }

                }

                
else
                
{
                    
return false;
                }

            }

            
            
public static bool CheckDate(string strInput)//日期格式,如:2001-1-1
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckTime(string strInput)//24小时制时间格式,23:19:59
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckDateTime(string strInput)//日期时间格式,如:2001-1-1 3:19:59
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckPhone(string strInput)//是否电话号码或传真号码,最长完整格式为:xxx(x)-xxxxxxx(x)-x(xxxxxxxx)
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^(\d{3,4})+(-(\d{7,8}))+(-(\d{1,9}))?$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckMobile(string strInput)//是否手机号码,最长完整格式为:(0)1xxxxxxxxxx
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^(0)?1\d{10}$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckFloat(string strInput)//是否实数,可以有+ -号开头
            {
                
try
                
{
                    
return Regex.IsMatch(strInput,@"^[+-]?\d+(\.\d+)?$");
                }

                
catch
                
{
                    
return false;
                }

            }

            
            
public static bool CheckPostalcode(string strInput)//是否邮政编码格式
            {
                
try
                
{
                    
return Regex.IsMatch(strInput, @"^\d{6}$");
                }

                
catch
                
{
                    
return false;
                }

            }

        
            
public static bool CheckEmail(string strInput)//是否是Email格式
            {
                
try
                
{
                    
return Regex.IsMatch(strInput, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 
                }

                
catch
                
{
                    
return false;
                }

            }

        }

    }

}
posted @ 2006-08-03 15:25  KenBlove  阅读(195)  评论(0编辑  收藏  举报