-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from marcoslcosta/master
Adicionando validação do telefone e refatorando
- Loading branch information
Showing
15 changed files
with
799 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Flunt.Br.Document.interfaces; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Flunt.Br.Document | ||
{ | ||
internal class CreditCard : IValidate | ||
{ | ||
public bool Validate(string value) | ||
{ | ||
value = new Regex(@"['\""&,\\]|\s{2,}").Replace(value, "").Trim(); | ||
if (!new Regex(@"[0-9]+").IsMatch(value)) return false; | ||
|
||
if (value.All(char.IsDigit) == false) | ||
{ | ||
return false; | ||
} | ||
if (12 > value.Length || value.Length > 19) | ||
{ | ||
return false; | ||
} | ||
|
||
return IsValidLuhnn(value); | ||
} | ||
|
||
private bool IsValidLuhnn(string val) | ||
{ | ||
int currentDigit; | ||
int valSum = 0; | ||
int currentProcNum = 0; | ||
|
||
for (int i = val.Length - 1; i >= 0; i--) | ||
{ | ||
if (!int.TryParse(val.Substring(i, 1), out currentDigit)) | ||
return false; | ||
|
||
currentProcNum = currentDigit << (1 + i & 1); | ||
valSum += (currentProcNum > 9 ? currentProcNum - 9 : currentProcNum); | ||
|
||
} | ||
|
||
return (valSum > 0 && valSum % 10 == 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,206 @@ | ||
using System.Text.RegularExpressions; | ||
using Flunt.Br.Document.interfaces; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Flunt.Br.Document | ||
{ | ||
internal class Phone : IValidate | ||
{ | ||
public bool Validate(string value) => new Regex(@"^\(\d{2}\)\d{4}-\d{4}$").Match(value).Success; | ||
|
||
public Phone() | ||
{ | ||
this.format = null; | ||
} | ||
public Phone(string numberFormat) | ||
{ | ||
var regex = new Regex(@"[0-9]+"); | ||
var phonePattern = Regex.Replace(numberFormat, @"[0-9,?]+", m => { | ||
var optionalsCount = m.Value.Split(new char[] { '?' }).Length - 1; | ||
var stringRegex = ""; | ||
if(optionalsCount != 0) | ||
{ | ||
stringRegex += (m.Length - optionalsCount); | ||
stringRegex += ","; | ||
stringRegex += (optionalsCount + (m.Length - optionalsCount)); | ||
} | ||
else | ||
{ | ||
stringRegex = m.Length.ToString(); | ||
} | ||
return $@"\d{{{stringRegex}}}"; | ||
}); | ||
phonePattern = @"^" + Regex.Replace(phonePattern, @"(?:(?(1)(?!))(\+)|(?(2)(?!))(\()|(?(3)(?!))(\)))+", m => $@"\{m.Value}").Replace("-", @"\-").Replace(" ", @"\s") + "$"; | ||
this.format = new Regex(phonePattern); | ||
} | ||
|
||
public bool Validate(string value) | ||
{ | ||
if (this.format != null) | ||
{ | ||
return this.format.IsMatch(value); | ||
} | ||
else | ||
{ | ||
foreach (var regex in this.ListPhoneValidFormat) | ||
{ | ||
if (regex.IsMatch(value)) return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
private IReadOnlyCollection<Regex> ListPhoneValidFormat | ||
{ | ||
get | ||
{ | ||
return new List<Regex> | ||
{ | ||
//(99) 9999-9999 | ||
new Regex(@"^\(\d{2}\)\s\d{4}\-\d{4}"), | ||
//(99) 99999-9999 | ||
new Regex(@"^\(\d{2}\)\s\d{5}-\d{4}$"), | ||
//(99)9999-9999 | ||
new Regex(@"^\(\d{2}\)\d{4}-\d{4}$"), | ||
//(99)99999-9999 | ||
new Regex(@"^\(\d{2}\)\d{5}-\d{4}$"), | ||
//999999-9999 | ||
new Regex(@"^\d{6}\-\d{4}$"), | ||
//9999999-9999 | ||
new Regex(@"^\d{7}\-\d{4}$"), | ||
//99999999999 | ||
new Regex(@"^\d{11}$"), | ||
//9999999999 | ||
new Regex(@"^\d{10}$"), | ||
//99 99999 9999 | ||
new Regex(@"^\d{2}\s\d{5}\s\d{4}$"), | ||
//99 9999 9999 | ||
new Regex(@"^\d{2}\s\d{4}\s\d{4}$"), | ||
//55(99) 9999-9999 | ||
new Regex(@"^\d{2}\(\d{2}\)\s\d{4}\-\d{4}$"), | ||
//55(99) 99999-9999 | ||
new Regex(@"^\d{2}\(\d{2}\)\s\d{5}\-\d{4}$"), | ||
//55(99)99999-9999 | ||
new Regex(@"^\d{2}\(\d{2}\)\d{5}\-\d{4}$"), | ||
//55(99)9999-9999 | ||
new Regex(@"^\d{2}\(\d{2}\)\d{4}\-\d{4}$"), | ||
//55999999-9999 | ||
new Regex(@"^\d{2}\d{2}\d{4}\-\d{4}$"), | ||
//5599 99999 9999 | ||
new Regex(@"^\d{4}\s\d{5}\s\d{4}$"), | ||
//5599 9999 9999 | ||
new Regex(@"^\d{4}\s\d{4}\s\d{4}$"), | ||
//5599 9999 9999 | ||
new Regex(@"^\d{4}\s\d{4}\s\d{4}$"), | ||
//55 (99) 9999-9999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\s\d{4}\-\d{4}$"), | ||
//55 (99) 9999 9999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\s\d{4}\s\d{4}$"), | ||
//55 (99) 99999-9999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\s\d{5}\-\d{4}$"), | ||
//55 (99) 999999999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\s\d{5}\d{4}$"), | ||
//55 (99)999999999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\d{5}\d{4}$"), | ||
//55 (99)99999999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\d{4}\d{4}$"), | ||
//55 (99)99999-9999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\d{5}\-\d{4}$"), | ||
//55 (99)9999-9999 | ||
new Regex(@"^\d{2}\s\(\d{2}\)\d{4}\-\d{4}$"), | ||
//55 999999-9999 | ||
new Regex(@"^\d{2}\s\d{2}\d{5}\-\d{4}$"), | ||
//55 9999999-9999 | ||
new Regex(@"^\d{2}\s\d{2}\d{5}\-\d{4}$"), | ||
//55 9999999 9999 | ||
new Regex(@"^\d{2}\s\d{2}\d{5}\s\d{4}$"), | ||
//55 999999-9999 | ||
new Regex(@"^\d{2}\s\d{2}\d{4}\-\d{4}$"), | ||
//55 999999 9999 | ||
new Regex(@"^\d{2}\s\d{2}\d{4}\s\d{4}$"), | ||
//55 9999999999 | ||
new Regex(@"^\d{2}\s\d{2}\d{4}\d{4}$"), | ||
//55 9999999999 | ||
new Regex(@"^\d{2}\s\d{2}\d{4}\d{4}$"), | ||
//559999999999 | ||
new Regex(@"^\d{2}\d{2}\d{4}\d{4}$"), | ||
//559999999999 | ||
new Regex(@"^\d{2}\d{2}\d{4}\d{4}$"), | ||
//559999999-9999 | ||
new Regex(@"^\d{2}\d{2}\d{5}\-\d{4}$"), | ||
//5599999999999 | ||
new Regex(@"^\d{2}\d{2}\d{5}\d{4}$"), | ||
//559999999999 | ||
new Regex(@"^\d{2}\d{2}\d{4}\d{4}$"), | ||
//55 99999999999 | ||
new Regex(@"^\d{2}\s\d{2}\d{5}\d{4}$"), | ||
//55 99 9999 9999 | ||
new Regex(@"^\d{2}\s\d{2}\s\d{4}\s\d{4}$"), | ||
//55 99 99999 9999 | ||
new Regex(@"^\d{2}\s\d{2}\s\d{5}\s\d{4}$"), | ||
//+55(99) 9999-9999 | ||
new Regex(@"^\+\d{2}\(\d{2}\)\s\d{4}\-\d{4}$"), | ||
//+55(99) 99999-9999 | ||
new Regex(@"^\+\d{2}\(\d{2}\)\s\d{5}\-\d{4}$"), | ||
//+55(99)99999-9999 | ||
new Regex(@"^\+\d{2}\(\d{2}\)\d{5}\-\d{4}$"), | ||
//+55(99)9999-9999 | ||
new Regex(@"^\+\d{2}\(\d{2}\)\d{4}\-\d{4}$"), | ||
//+55999999-9999 | ||
new Regex(@"^\+\d{2}\d{2}\d{4}\-\d{4}$"), | ||
//+559999999-9999 | ||
new Regex(@"^\+\d{2}\d{2}\d{5}\-\d{4}$"), | ||
//+5599 99999 9999 | ||
new Regex(@"^\+\d{4}\s\d{5}\s\d{4}$"), | ||
//+5599 9999 9999 | ||
new Regex(@"^\+\d{4}\s\d{4}\s\d{4}$"), | ||
//+5599 9999 9999 | ||
new Regex(@"^\+\d{4}\s\d{4}\s\d{4}$"), | ||
//+55 (99) 9999-9999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\s\d{4}\-\d{4}$"), | ||
//+55 (99) 9999 9999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\s\d{4}\s\d{4}$"), | ||
//+55 (99) 999999999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\s\d{5}\d{4}$"), | ||
//+55 (99)999999999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\d{5}\d{4}$"), | ||
//+55 (99)99999999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\d{4}\d{4}$"), | ||
//+55 (99)99999-9999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\d{5}\-\d{4}$"), | ||
//+55 (99)9999-9999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\d{4}\-\d{4}$"), | ||
//+55 999999-9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{5}\-\d{4}$"), | ||
//+55 9999999-9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{5}\-\d{4}$"), | ||
//+55 9999999 9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{5}\s\d{4}$"), | ||
//+55 999999-9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{4}\-\d{4}$"), | ||
//+55 999999 9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{4}\s\d{4}$"), | ||
//+55 9999999999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{4}\d{4}$"), | ||
//+55 9999999999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{4}\d{4}$"), | ||
//+559999999999 | ||
new Regex(@"^\+\d{2}\d{2}\d{4}\d{4}$"), | ||
//+559999999999 | ||
new Regex(@"^\+\d{2}\d{2}\d{4}\d{4}$"), | ||
//+55 99999999999 | ||
new Regex(@"^\+\d{2}\s\d{2}\d{5}\d{4}$"), | ||
//+55 99 99999 9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\s\d{5}\s\d{4}$"), | ||
//+55 99 9999 9999 | ||
new Regex(@"^\+\d{2}\s\d{2}\s\d{4}\s\d{4}$"), | ||
//+55 (99) 99999-9999 | ||
new Regex(@"^\+\d{2}\s\(\d{2}\)\s\d{5}\-\d{4}$"), | ||
}; | ||
} | ||
|
||
} | ||
|
||
private Regex format { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Flunt.Br.Document.interfaces; | ||
using System.Collections.Generic; | ||
|
||
namespace Flunt.Br.Document | ||
{ | ||
internal class VoterDocument : IValidate | ||
{ | ||
public bool Validate(string value) | ||
{ | ||
int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, DV1, DV2, UltDig; | ||
|
||
value = new Regex(@"['\""&,\\]|\s{2,}").Replace(value, "").Trim(); | ||
if (!new Regex(@"[0-9]+").IsMatch(value)) return false; | ||
if ((value.Length < 12)) | ||
{ | ||
value = value.PadLeft(12 - value.Length, '0'); | ||
} | ||
|
||
UltDig = value.Length; | ||
if (value == "000000000000") | ||
{ | ||
return false; | ||
} | ||
|
||
d1 = int.Parse(value.Substring(UltDig - 12, 1)); | ||
d2 = int.Parse(value.Substring(UltDig - 11, 1)); | ||
d3 = int.Parse(value.Substring(UltDig - 10, 1)); | ||
d4 = int.Parse(value.Substring(UltDig - 9, 1)); | ||
d5 = int.Parse(value.Substring(UltDig - 8, 1)); | ||
d6 = int.Parse(value.Substring(UltDig - 7, 1)); | ||
d7 = int.Parse(value.Substring(UltDig - 6, 1)); | ||
d8 = int.Parse(value.Substring(UltDig - 5, 1)); | ||
d9 = int.Parse(value.Substring(UltDig - 4, 1)); | ||
d10 = int.Parse(value.Substring(UltDig - 3, 1)); | ||
d11 = int.Parse(value.Substring(UltDig - 2, 1)); | ||
d12 = int.Parse(value.Substring(UltDig - 1, 1)); | ||
DV1 = ((d1 * 2) | ||
+ ((d2 * 3) | ||
+ ((d3 * 4) | ||
+ ((d4 * 5) | ||
+ ((d5 * 6) | ||
+ ((d6 * 7) | ||
+ ((d7 * 8) | ||
+ (d8 * 9)))))))); | ||
DV1 = (DV1 % 11); | ||
|
||
if ((DV1 == 10)) | ||
{ | ||
DV1 = 0; | ||
} | ||
|
||
DV2 = ((d9 * 7) | ||
+ ((d10 * 8) | ||
+ (DV1 * 9))); | ||
DV2 = (DV2 % 11); | ||
// Obtem o resto | ||
// se o resto for igual a 10 altera pra 0 | ||
if ((DV2 == 10)) | ||
{ | ||
DV2 = 0; | ||
} | ||
|
||
if ((d11 == DV1) && (d12 == DV2)) | ||
{ | ||
if ((d9 + d10) > 0 && (d9 + d10) < 29) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using Flunt.Br.Document; | ||
using Flunt.Validations; | ||
|
||
namespace Flunt.Br.Validation | ||
{ | ||
public static partial class ContractExtensions | ||
{ | ||
public static Contract IsCreditCard(this Contract contract, string value, string property, string message) | ||
{ | ||
|
||
if (!new CreditCard().Validate(value)) | ||
contract.AddNotification(property, message); | ||
return contract; | ||
} | ||
} | ||
} |
Oops, something went wrong.