Răsfoiți Sursa

匹配固话号码

懒得勤快 2 ani în urmă
părinte
comite
2c4bbff59b

+ 11 - 1
Masuit.Tools.Abstractions/Extensions/BaseType/StringExtensions.cs

@@ -617,7 +617,17 @@ namespace Masuit.Tools
         /// <returns>是否匹配成功</returns>
         public static bool MatchPhoneNumber(this string s)
         {
-            return !string.IsNullOrEmpty(s) && s[0] == '1' && (s[1] > '2' || s[1] <= '9');
+            return !string.IsNullOrEmpty(s) && s.Length == 11 && s[0] == '1' && (s[1] > '2' || s[1] <= '9');
+        }
+
+        /// <summary>
+        /// 匹配固话号码
+        /// </summary>
+        /// <param name="s">源字符串</param>
+        /// <returns>是否匹配成功</returns>
+        public static bool MatchLandline(this string s)
+        {
+            return Regex.IsMatch(s, @"^0\d{2,3}(?:-?\d{8}|-?\d{7})$");
         }
 
         #endregion 校验手机号码的正确性

+ 58 - 0
Masuit.Tools.Abstractions/Validator/IsLandlineAttribute.cs

@@ -0,0 +1,58 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator;
+
+/// <summary>
+/// 验证固定电话是否合法
+/// </summary>
+public class IsLandlineAttribute : ValidationAttribute
+{
+    /// <summary>
+    /// 是否允许为空
+    /// </summary>
+    public bool AllowEmpty { get; set; }
+
+    private readonly string _customMessage;
+
+    /// <summary>
+    ///
+    /// </summary>
+    /// <param name="customMessage">自定义错误消息</param>
+    public IsLandlineAttribute(string customMessage = null)
+    {
+        _customMessage = customMessage;
+    }
+
+    /// <summary>
+    /// 验证手机号码是否合法
+    /// </summary>
+    /// <param name="value"></param>
+    /// <returns></returns>
+    public override bool IsValid(object value)
+    {
+        if (AllowEmpty)
+        {
+            switch (value)
+            {
+                case null:
+                case string s when string.IsNullOrEmpty(s):
+                    return true;
+            }
+        }
+
+        if (value is null)
+        {
+            ErrorMessage = _customMessage ?? "固定电话不能为空";
+            return false;
+        }
+
+        string phone = value as string;
+        if (phone.MatchLandline())
+        {
+            return true;
+        }
+
+        ErrorMessage = _customMessage ?? "固定电话格式不正确,请输入有效的大陆11/12位固定电话!";
+        return false;
+    }
+}

+ 22 - 1
Test/Masuit.Tools.Test/ExtensionMethodsTest.cs

@@ -20,6 +20,7 @@ namespace Masuit.Tools.Test
             var (expect, match) = "[email protected]".MatchEmail();
             Assert.AreEqual(true, expect);
         }
+
         [TestMethod]
         public void MatchIdentifyCard_False()
         {
@@ -34,5 +35,25 @@ namespace Masuit.Tools.Test
         {
             Xunit.Assert.True(phone.MatchPhoneNumber());
         }
+
+        [Theory]
+        [InlineData("166666666666")]
+        [InlineData("199999999996")]
+        public void CanNot_MatchPhoneNumber_(string phone)
+        {
+            Xunit.Assert.False(phone.MatchPhoneNumber());
+        }
+
+        [Theory]
+        [InlineData("010-12345678")]
+        [InlineData("0731-87654321")]
+        [InlineData("0351-7654321")]
+        [InlineData("01012345678")]
+        [InlineData("073187654321")]
+        [InlineData("03517654321")]
+        public void Can_MatchLandline_(string phone)
+        {
+            Xunit.Assert.True(phone.MatchLandline());
+        }
     }
-}
+}

+ 2 - 2
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -97,10 +97,10 @@
       <Version>4.18.4</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestAdapter">
-      <Version>3.0.4</Version>
+      <Version>3.1.1</Version>
     </PackageReference>
     <PackageReference Include="MSTest.TestFramework">
-      <Version>3.0.4</Version>
+      <Version>3.1.1</Version>
     </PackageReference>
     <PackageReference Include="NUnit">
       <Version>3.13.3</Version>