Created
November 12, 2016 12:53
-
-
Save AndreasLoukakis/28362bbdb31cc8a5edcbb04cf8045fb3 to your computer and use it in GitHub Desktop.
ΑΜΚΑ validator for angular2
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 {AbstractControl} from '@angular/forms'; | |
export class AmkaValidator { | |
public static validate(c:AbstractControl) { | |
let isElevenDigitNumber = /^\d{11}$/.test(c.value), | |
string_amka = c.value.toString(), | |
sumIsValid = false, | |
sum = 0; | |
for (let i=1; i<= string_amka.length; i++) { | |
let digit = parseInt(string_amka[i-1], 10); | |
digit = i % 2 === 0 ? digit * 2 : digit; | |
sum += digit > 9 ? (digit - 9) : digit; | |
} | |
sumIsValid = sum % 10 === 0 && sum > 0; | |
return sumIsValid && isElevenDigitNumber ? null : { | |
validateAmka: { | |
valid: false | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment