diff --git a/.gitignore b/.gitignore index 83be766..6c3cbdb 100644 --- a/.gitignore +++ b/.gitignore @@ -190,4 +190,7 @@ FakesAssemblies/ *.plg # Visual Studio 6 workspace options file -*.opt \ No newline at end of file +*.opt + +#JetBrains Rider +.idea/ \ No newline at end of file diff --git a/Validator.UnitTest/ValidatorTest.cs b/Validator.UnitTest/ValidatorTest.cs index 0420777..8c2ee06 100644 --- a/Validator.UnitTest/ValidatorTest.cs +++ b/Validator.UnitTest/ValidatorTest.cs @@ -843,5 +843,25 @@ public void IsMobilePhone(string input, string locale, bool expected) var actual = Validator.IsMobilePhone(input, locale); Assert.Equal(expected, actual); } + + [Theory] + [InlineData("aa:aa:aa:aa:aa:aa", true)] + [InlineData("1a:2b:3c:4d:5e:6f", true)] + [InlineData("00:00:00:00:00:00", true)] + [InlineData("00 00 00 00 00 00", true)] + [InlineData("AA-11-BB-22-CC-33", true)] + [InlineData("ABCD.1234.5678", true)] + [InlineData("qwertyuiop", false)] + [InlineData("zz:zz:yy:yy:xx:xx", false)] + [InlineData("a1@b2@c3@d4@e5@6f", false)] + [InlineData("aa:aa:aa:aa:aa", false)] + [InlineData("00:00-00:00-00:00", false)] + [InlineData("AA-11-BZ-22-CC-33", false)] + [InlineData("AA 11 BB 22 CC33", false)] + public void IsMacAddress(string input, bool expected) + { + var actual = Validator.IsMacAddress(input); + Assert.Equal(expected, actual); + } } } diff --git a/Validator/Validator.cs b/Validator/Validator.cs index 867412a..587366a 100644 --- a/Validator/Validator.cs +++ b/Validator/Validator.cs @@ -316,5 +316,25 @@ public static bool IsMongoId(string input) => /// public static bool IsByteLength(string input, int min, int max = int.MaxValue) => input.Length >= min && input.Length <= max; + + /// + /// Determine whether input is a valid MAC address + /// + /// + /// + public static bool IsMacAddress(string input) + { + const string macAddress = "^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$"; + const string macAddressNoColons = "^([0-9a-fA-F]){12}$"; + const string macAddressWithHyphen = "^([0-9a-fA-F]{2}\\-){5}([0-9a-fA-F]{2})$"; + const string macAddressWithSpaces = "^([0-9a-fA-F]{2}\\s){5}([0-9a-fA-F]{2})$"; + const string macAddressWithDots = "^([0-9a-fA-F]{4})\\.([0-9a-fA-F]{4})\\.([0-9a-fA-F]{4})$"; + + return Regex.IsMatch(input, macAddress) || + Regex.IsMatch(input, macAddressNoColons) || + Regex.IsMatch(input, macAddressWithHyphen) || + Regex.IsMatch(input, macAddressWithSpaces) || + Regex.IsMatch(input, macAddressWithDots); + } } }