Skip to content

Instantly share code, notes, and snippets.

@AndreasLoukakis
Created November 12, 2016 12:53
Show Gist options
  • Save AndreasLoukakis/28362bbdb31cc8a5edcbb04cf8045fb3 to your computer and use it in GitHub Desktop.
Save AndreasLoukakis/28362bbdb31cc8a5edcbb04cf8045fb3 to your computer and use it in GitHub Desktop.
ΑΜΚΑ validator for angular2
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