Last active
December 17, 2018 14:21
-
-
Save afmicc/730d2ec6009ad8f4574b53997206d2b7 to your computer and use it in GitHub Desktop.
Verificador de cedula de identidad (CI) uruguaya. Angular validator.
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
namespace Helpers | |
{ | |
#region --- Usings --- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
#endregion | |
public static class IdentificationValidator | |
{ | |
public static bool IsUruguayanFormat(string identification) | |
{ | |
try | |
{ | |
if (string.IsNullOrWhiteSpace(identification) || identification.Length != 8 || !int.TryParse(identification, out int identificationNumber)) | |
return false; | |
int[] arrayCI = identification | |
.Select(x => int.TryParse(x.ToString(), out int y) ? y : -1) | |
.ToArray(); | |
int[] arrayRef = "2987634" | |
.Select(x => int.TryParse(x.ToString(), out int y) ? y : -1) | |
.ToArray(); | |
int inputVerificationNumber = arrayCI[7]; | |
int verificationNumberRaw = 0; | |
for (int i = 0; i < arrayRef.Length; i++) { | |
verificationNumberRaw = verificationNumberRaw + arrayCI[i] * arrayRef[i]; | |
} | |
int calculatedVerificationNumber = 10 - (verificationNumberRaw % 10); | |
if (calculatedVerificationNumber != inputVerificationNumber) { | |
return false; | |
} | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
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
import { FormControl } from '@angular/forms'; | |
export function uruguayanIdentificationValidator(control: FormControl): { [uruguayanIdentification: string]: boolean } { | |
const identification = control.value; | |
const invalidReturn = { uruguayanIdentification: true }; | |
if (!identification || identification.length !== 8 || isNaN(identification)) { | |
return invalidReturn; | |
} | |
const arrayCI: number[] = (<string>identification).split('').map(x => +x); | |
const arrayRef: number[] = '2987634'.split('').map(x => +x); | |
const inputVerificationNumber: number = arrayCI[7]; | |
let verificationNumberRaw = 0; | |
for (let i = 0; i < arrayRef.length; i++) { | |
verificationNumberRaw = verificationNumberRaw + arrayCI[i] * arrayRef[i]; | |
} | |
const calculatedVerificationNumber: number = 10 - (verificationNumberRaw % 10); | |
if (calculatedVerificationNumber !== inputVerificationNumber) { | |
return invalidReturn; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment