Last active
February 21, 2019 12:30
-
-
Save pcg92/54014a204bd341b15c895ab6d42e4c14 to your computer and use it in GitHub Desktop.
Validador DNI en dart / NIF validator dart
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
bool validateNIF(String value){ | |
var validChars = 'TRWAGMYFPDXBNJZSQVHLCKET'; | |
RegExp nifRegex = new RegExp(r'^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]$'); | |
var str = value.toString().toUpperCase(); | |
if (!nifRegex.hasMatch(str)) return false; | |
String nie = str | |
.replaceAll(new RegExp(r"/^[X]/"), '0') | |
.replaceAll(new RegExp(r"/^[Y]/"), '1') | |
.replaceAll(new RegExp(r"/^[Z]/"), '2'); | |
var letter = str.substring(str.length-1); | |
var charIndex = int.parse(nie.substring(0, 8)) % 23; | |
return validChars[charIndex] == letter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment