Skip to content

Commit

Permalink
Functions included:
Browse files Browse the repository at this point in the history
HasNumbersOnly
IsIntNumber
IsLongNumber
IsDecimalNumber

Brazil functions included:
IsMobileNumber (with area code validation)
  • Loading branch information
Bruno de Souza Melo committed May 27, 2024
1 parent 8107e7c commit 4fb1f95
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/NuvTools.Validation/Brazil/RegexPattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NuvTools.Validation.Brazil;

/// <summary>
/// Regex patterns class.
/// </summary>
public static class RegexPattern
{
/// <summary>
/// Regex pattern for mobile phone validation.
/// </summary>
public const string MobileNumber = @"(?<CodeArea>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)(?<FirstDigit>9)(?<Number>\d{8})";
}
18 changes: 18 additions & 0 deletions src/NuvTools.Validation/Brazil/Validator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.RegularExpressions;

namespace NuvTools.Validation.Brazil;


Expand Down Expand Up @@ -109,4 +111,20 @@ public static bool IsCNPJ(this string value)
return value.EndsWith(digit);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="mobileNumber">Mobile number.</param>
/// <param name="clearMask">Clear mask before validate.</param>
/// <returns></returns>
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;
}

}
4 changes: 2 additions & 2 deletions src/NuvTools.Validation/NuvTools.Validation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6;net7;net8</TargetFrameworks>
<TargetFrameworks>net7;net8</TargetFrameworks>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Nuv Tools</Authors>
Expand All @@ -10,7 +10,7 @@
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Validation.snk</AssemblyOriginatorKeyFile>
<Description>Validation library for Web, Desktop and Mobile (MAUI) applications.</Description>
<Version>8.0.1</Version>
<Version>8.1.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageIcon>icon.png</PackageIcon>
Expand Down
62 changes: 62 additions & 0 deletions src/NuvTools.Validation/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,66 @@ public static List<string> Validate<T>(this T obj)

return null;
}

/// <summary>
/// Checks if the provided string contains only numeric characters.
/// </summary>
/// <param name="input">The string to check.</param>
/// <returns>True if the input contains only numeric characters, otherwise false.</returns>
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);
}

/// <summary>
/// Checks if the provided string represents a long type number.
/// </summary>
/// <param name="input">The string to check.</param>
/// <param name="positiveOnly">If only positive numbers is valid.</param>
/// <returns>True if the input is a whole number, otherwise false.</returns>
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;
}

/// <summary>
/// Checks if the provided string represents a long type number.
/// </summary>
/// <param name="input">The string to check.</param>
/// <param name="positiveOnly">If only positive numbers is valid.</param>
/// <returns>True if the input is a whole number, otherwise false.</returns>
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;
}

/// <summary>
/// Checks if the provided string represents a valid decimal number.
/// </summary>
/// <param name="input">The string to check.</param>
/// <param name="positiveOnly">If only positive numbers is valid.</param>
/// <param name="provider">An object that supplies culture-specific parsing information about s.</param>
/// <returns>True if the input is a valid decimal number, otherwise false.</returns>
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace NuvTools.Validation.Tests.Brazil;

[TestFixture()]
public class CNPJandCPFExtensions
public class BrazilValidatorExtensions
{
[Test()]
public void ValidateCNPJ()
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Version>8.0.1</Version>
<Version>8.1.0</Version>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2024 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuvtools.com</PackageProjectUrl>
Expand All @@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 50 additions & 0 deletions tests/NuvTools.Validation.Test/ValidatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 4fb1f95

Please sign in to comment.