ASP.NET 常用的正则表达式(使用C#3.0扩展方法)

本文介绍了一组C#扩展方法,用于验证不同类型的输入数据,包括电子邮件地址、URL、日期时间格式等,并提供了详细的正则表达式实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Text.RegularExpressions;

 

/// <summary>

/// 使用C#3.0扩展方法(Extension Methods)实现验证功能

/// </summary>

public static class Extensions

{

    public static bool IsValidEmailAddress(this string email)

    {

        Regex regex = new Regex(@"/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*");

        return regex.IsMatch(email);

    }

 

    public static bool IsValidURL(this string url)

    {

        Regex regex = new Regex(@"http(s)?://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?");

        return regex.IsMatch(url);

    }

 

    public static bool OnlyCharacters(this string character)

    {

        Regex regex = new Regex(@"^.[A-Za-z]+$");

        return regex.IsMatch(character);

    }

 

    public static bool OnlyNumber(this string number)

    {

        Regex regex = new Regex(@"^.[0-9]*$");

        return regex.IsMatch(number);

    }

 

    public static bool IsValidDate(this string date)

    {   //验证日期类型为yyyy-MM-dd

        Regex regex = new Regex(@"^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1/d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$");

        return regex.IsMatch(date);

    }

 

    public static bool IsValidDateTime(this string dateTime)

    {   //验证日期类型为yyyy-MM-dd hh:mm:ss

        Regex regex = new Regex(@"^((((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$");

        return regex.IsMatch(dateTime);

    }

 

    public static bool IsValidUSPhone(this string phone)

    {

        Regex regex = new Regex(@"((/(/d{3}/) ?)|(/d{3}-))?/d{3}-/d{4}");

        return regex.IsMatch(phone);

    }

 

    public static bool IsValidUSZipCode(this string zipcode)

    {

        Regex regex = new Regex(@"/d{5}(-/d{4})?");

        return regex.IsMatch(zipcode);

    }

 

    public static bool IsValidKorean(this string korean)

    {

        Regex regex = new Regex(@"^.[/uac00-/ud7af/u1100-/u11FF/u3130-/u318f]+$");

        return regex.IsMatch(korean);

    }

 

    public static bool IsValidCNMobile(this string mobile)

    {

        Regex regex = new Regex(@"^((/(/d{3}/))|(/d{3}/-))?13[0-9]/d{8}|15[0-9]/d{8}|18[0-9]/d{8}");

        return regex.IsMatch(mobile);

    }

 

    public static bool IsValidCNPhone(this string phone)

    {

        Regex regex = new Regex(@"(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}");

        return regex.IsMatch(phone);

    }

 

    public static bool IsValidCNZipCode(this string zipcode)

    {

        Regex regex = new Regex(@"d{6}");

        return regex.IsMatch(zipcode);

    }

 

    public static bool IsValidCNID(this string ID)

    {   //验证身份证是否为15位或18

        Regex regex = new Regex(@"d{18}|d{15}");

        return regex.IsMatch(ssn);

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值