netCore 封装一个检验邮箱的类

发布时间 2024-01-06 18:23:32作者: 流浪のwolf
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQL_Common.ValidateRules
{
    /// <summary> 
    /// 如果要扩展一个规则,就新增一个类,
    /// 直接继承实现 BaseAbstractAttribute 抽象类即可
    /// </summary>
    public class ZxEmailAttribute: BaseAbstractAttribute
    {
        public ZxEmailAttribute(string? messge) : base(messge)
        {
        }
        public override (bool, string?) DoValidate(object oValue)
        {
            //这里就是校验邮箱的业务逻辑
            if (oValue == null)
            {
                return (false, "邮箱地址不能为空");
            }
            string str = @"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(com|cn|net)$";
            bool bResult = System.Text.RegularExpressions.Regex.IsMatch(oValue.ToString(), str);
            return bResult ? (true, string.Empty) : (false, Message);
        }
    }
}