private static bool IsValidNIF(string nif) { if (string.IsNullOrWhiteSpace(nif) || !Regex.IsMatch(nif, "^[0-9]+$") || nif.Length != 9) return false; char c = nif[0]; //Check first number is (5) apenas empresas //if (!c.Equals('5')) // return false; //Perform CheckDigit calculations int checkDigit = (Convert.ToInt32(c.ToString()) * 9); for (int i = 2; i <= 8; i++) { checkDigit += Convert.ToInt32(nif[i - 1].ToString()) * (10 - i); } checkDigit = 11 - (checkDigit % 11); //if checkDigit is higher than ten set it to zero if (checkDigit >= 10) checkDigit = 0; //Compare checkDigit with the last number of NIF //If equal the NIF is Valid. if (checkDigit.ToString() != nif[8].ToString()) return false; return true; }
Wednesday, October 19, 2011
Validate Portuguese NIF
Labels:
Validation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment