Skip to content

Instantly share code, notes, and snippets.

@pcg92
Last active February 21, 2019 12:30
Show Gist options
  • Save pcg92/54014a204bd341b15c895ab6d42e4c14 to your computer and use it in GitHub Desktop.
Save pcg92/54014a204bd341b15c895ab6d42e4c14 to your computer and use it in GitHub Desktop.
Validador DNI en dart / NIF validator dart
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