From 4fb1f956988fe7732be9aa0ece49f56b3c66a611 Mon Sep 17 00:00:00 2001 From: Bruno de Souza Melo Date: Mon, 27 May 2024 17:00:50 -0300 Subject: [PATCH] Functions included: HasNumbersOnly IsIntNumber IsLongNumber IsDecimalNumber Brazil functions included: IsMobileNumber (with area code validation) --- .../Brazil/RegexPattern.cs | 12 ++++ src/NuvTools.Validation/Brazil/Validator.cs | 18 ++++++ .../NuvTools.Validation.csproj | 4 +- src/NuvTools.Validation/Validator.cs | 62 +++++++++++++++++++ ...nsions.cs => BrazilValidatorExtensions.cs} | 14 ++++- .../NuvTools.Validation.Tests.csproj | 4 +- .../ValidatorExtensions.cs | 50 +++++++++++++++ 7 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 src/NuvTools.Validation/Brazil/RegexPattern.cs rename tests/NuvTools.Validation.Test/Brazil/{CNPJandCPFExtensions.cs => BrazilValidatorExtensions.cs} (55%) create mode 100644 tests/NuvTools.Validation.Test/ValidatorExtensions.cs diff --git a/src/NuvTools.Validation/Brazil/RegexPattern.cs b/src/NuvTools.Validation/Brazil/RegexPattern.cs new file mode 100644 index 0000000..b6f1cf2 --- /dev/null +++ b/src/NuvTools.Validation/Brazil/RegexPattern.cs @@ -0,0 +1,12 @@ +namespace NuvTools.Validation.Brazil; + +/// +/// Regex patterns class. +/// +public static class RegexPattern +{ + /// + /// Regex pattern for mobile phone validation. + /// + public const string MobileNumber = @"(?11|12|13|14|15|16|17|18|19|21|22|24|27|28|31|32|33|34|35|37|38|41|42|43|44|45|46|47|48|49|51|53|54|55|61|62|63|64|65|66|67|68|69|71|73|74|75|77|79|81|82|83|84|85|86|87|88|89|91|92|93|94|95|96|97|98|99)(?9)(?\d{8})"; +} \ No newline at end of file diff --git a/src/NuvTools.Validation/Brazil/Validator.cs b/src/NuvTools.Validation/Brazil/Validator.cs index 479a65c..0d68b6d 100644 --- a/src/NuvTools.Validation/Brazil/Validator.cs +++ b/src/NuvTools.Validation/Brazil/Validator.cs @@ -1,3 +1,5 @@ +using System.Text.RegularExpressions; + namespace NuvTools.Validation.Brazil; @@ -109,4 +111,20 @@ public static bool IsCNPJ(this string value) return value.EndsWith(digit); } + /// + /// Validates a mobile phone number based on a specific regex pattern. The method checks if the phone number contains a valid Brazilian Code Area, followed by the digit 9 and then exactly eight digits. + /// + /// Mobile number. + /// Clear mask before validate. + /// + public static bool IsMobileNumber(this string mobileNumber, bool clearMask = true) + { + if (clearMask) + mobileNumber = mobileNumber.GetNumbersOnly(); + + var regex = new Regex(RegexPattern.MobileNumber); + var match = regex.Match(mobileNumber); + return match.Success; + } + } \ No newline at end of file diff --git a/src/NuvTools.Validation/NuvTools.Validation.csproj b/src/NuvTools.Validation/NuvTools.Validation.csproj index 9bdfd4d..63bb4e1 100644 --- a/src/NuvTools.Validation/NuvTools.Validation.csproj +++ b/src/NuvTools.Validation/NuvTools.Validation.csproj @@ -1,7 +1,7 @@ - net6;net7;net8 + net7;net8 latest true Nuv Tools @@ -10,7 +10,7 @@ True NuvTools.Validation.snk Validation library for Web, Desktop and Mobile (MAUI) applications. - 8.0.1 + 8.1.0 True True icon.png diff --git a/src/NuvTools.Validation/Validator.cs b/src/NuvTools.Validation/Validator.cs index 84f3be2..99f29d8 100644 --- a/src/NuvTools.Validation/Validator.cs +++ b/src/NuvTools.Validation/Validator.cs @@ -32,4 +32,66 @@ public static List Validate(this T obj) return null; } + + /// + /// Checks if the provided string contains only numeric characters. + /// + /// The string to check. + /// True if the input contains only numeric characters, otherwise false. + public static bool HasNumbersOnly(this string input) + { + // Verifies if the string is null or empty + if (string.IsNullOrEmpty(input)) return false; + + // Checks if all characters in the string are digits + return input.All(char.IsDigit); + } + + /// + /// Checks if the provided string represents a long type number. + /// + /// The string to check. + /// If only positive numbers is valid. + /// True if the input is a whole number, otherwise false. + public static bool IsIntNumber(this string input, bool positiveOnly = false) + { + // Try to parse the input string as int + if (int.TryParse(input, out int number)) + return positiveOnly ? int.IsPositive(number) : true; + + // Return false if parsing fails or number is negative + return false; + } + + /// + /// Checks if the provided string represents a long type number. + /// + /// The string to check. + /// If only positive numbers is valid. + /// True if the input is a whole number, otherwise false. + public static bool IsLongNumber(this string input, bool positiveOnly = false) + { + // Try to parse the input string as long + if (long.TryParse(input, out long number)) + return positiveOnly ? long.IsPositive(number) : true; + + // Return false if parsing fails or number is negative + return false; + } + + /// + /// Checks if the provided string represents a valid decimal number. + /// + /// The string to check. + /// If only positive numbers is valid. + /// An object that supplies culture-specific parsing information about s. + /// True if the input is a valid decimal number, otherwise false. + public static bool IsDecimalNumber(this string input, bool positiveOnly = false, IFormatProvider provider = null) + { + // Try to parse the input string as a decimal + if (decimal.TryParse(input, System.Globalization.NumberStyles.Number | System.Globalization.NumberStyles.AllowDecimalPoint, provider, out decimal number)) + return positiveOnly ? decimal.IsPositive(number) : true; + + return false; + } } \ No newline at end of file diff --git a/tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs b/tests/NuvTools.Validation.Test/Brazil/BrazilValidatorExtensions.cs similarity index 55% rename from tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs rename to tests/NuvTools.Validation.Test/Brazil/BrazilValidatorExtensions.cs index 4450786..3e1143b 100644 --- a/tests/NuvTools.Validation.Test/Brazil/CNPJandCPFExtensions.cs +++ b/tests/NuvTools.Validation.Test/Brazil/BrazilValidatorExtensions.cs @@ -4,7 +4,7 @@ namespace NuvTools.Validation.Tests.Brazil; [TestFixture()] -public class CNPJandCPFExtensions +public class BrazilValidatorExtensions { [Test()] public void ValidateCNPJ() @@ -24,4 +24,16 @@ public void ValidateCPF() Assert.That("83289988074".IsCPF()); } + [Test()] + public void ValidateMobileNumber() + { + Assert.That("61944446666".IsMobileNumber()); + Assert.That("21955557777".IsMobileNumber()); + Assert.That("(21) 95555-7777".IsMobileNumber()); + Assert.That(!"(21) 95555-7777".IsMobileNumber(false)); + Assert.That(!"erro".IsMobileNumber()); + Assert.That(!"994645".IsMobileNumber()); + Assert.That(!"21866664444".IsMobileNumber()); + } + } \ No newline at end of file diff --git a/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj b/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj index 0528601..9509b5f 100644 --- a/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj +++ b/tests/NuvTools.Validation.Test/NuvTools.Validation.Tests.csproj @@ -3,7 +3,7 @@ net8.0 false - 8.0.1 + 8.1.0 Nuv Tools Copyright © 2024 Nuv Tools https://nuvtools.com @@ -14,7 +14,7 @@ - + diff --git a/tests/NuvTools.Validation.Test/ValidatorExtensions.cs b/tests/NuvTools.Validation.Test/ValidatorExtensions.cs new file mode 100644 index 0000000..35c1ec5 --- /dev/null +++ b/tests/NuvTools.Validation.Test/ValidatorExtensions.cs @@ -0,0 +1,50 @@ +using NUnit.Framework; +using System.Globalization; + +namespace NuvTools.Validation.Tests; + +[TestFixture()] +public class ValidatorExtensions +{ + [Test()] + public void ValidateHasNumbersOnly() + { + Assert.That("12345678910123456789101234567891012345678910123456789101234567891012345678910".HasNumbersOnly()); + Assert.That("12345678910".HasNumbersOnly()); + Assert.That(!"03.785.417".HasNumbersOnly()); + Assert.That(!"-54243121000193".HasNumbersOnly()); + } + + [Test()] + public void ValidateIsIntNumber() + { + Assert.That("111111111".IsIntNumber()); + Assert.That(!"11111111111".IsIntNumber()); + Assert.That("-111111111".IsIntNumber()); + Assert.That(!"-111111111".IsIntNumber(true)); + Assert.That(!"erro".IsLongNumber()); + Assert.That(!"555.666.777-00".IsIntNumber()); + } + + [Test()] + public void ValidateIsLongNumber() + { + Assert.That("11111111111".IsLongNumber()); + Assert.That("-11111111111".IsLongNumber()); + Assert.That(!"-11111111111".IsLongNumber(true)); + Assert.That(!"erro".IsLongNumber()); + Assert.That(!"555.666.777-00".IsLongNumber()); + } + + [Test()] + public void ValidateIsDecimalNumber() + { + Assert.That($"583{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}08".IsDecimalNumber()); + Assert.That("11111111111".IsDecimalNumber()); + Assert.That("-11111111111".IsDecimalNumber()); + Assert.That(!"-11111111111".IsDecimalNumber(true)); + Assert.That(!"erro".IsDecimalNumber()); + Assert.That("83289988074".IsDecimalNumber()); + } + +} \ No newline at end of file