/// <summary> /// Verifies if a given RG ("Registro Geral") is valid. /// </summary> /// <param name="_rg">The RG number as a string.</param> /// <returns>'true' if the RG is valid, 'false' otherwise.</returns> public static bool IsValidRG(string _rg) { if (!string.IsNullOrEmpty(_rg)) { int sum = 0; string rg = Format.AsDigitsOnly(_rg); int[] factors = new int[9] { 2, 3, 4, 5, 6, 7, 8, 9, 100 }; if (rg.Length != 9) { return(false); } for (int i = 0; i < 9; i++) { sum += int.Parse(rg[i].ToString()) * factors[i]; } return((sum % 11) == 0); } return(false); }
/// <summary> /// Verifies if a given CPF ("Cadastro de Pessoa Física") is valid. /// </summary> /// <param name="_cpf">The RG number as a string.</param> /// <returns>'true' if the RG is valid, 'false' otherwise.</returns> public static bool IsValidCPF(string _cpf) { if (!string.IsNullOrEmpty(_cpf)) { int sum = 0; int rest = 0; string auxCpf = string.Empty; string digits = string.Empty; int[] factors1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 }; int[] factors2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 }; _cpf = Format.AsDigitsOnly(_cpf); if (_cpf.Length != 11) { return(false); } auxCpf = _cpf.Substring(0, 9); for (int i = 0; i < 9; i++) { sum += int.Parse(auxCpf[i].ToString()) * factors1[i]; } rest = sum % 11; rest = (rest >= 2) ? 11 - rest : 0; sum = 0; digits += rest; auxCpf += digits; for (int i = 0; i < 10; i++) { sum += int.Parse(auxCpf[i].ToString()) * factors2[i]; } rest = sum % 11; rest = (rest >= 2) ? 11 - rest : 0; digits += rest; return(_cpf.EndsWith(digits)); } return(false); }
/// <summary> /// Verifies if a given CNPJ ("Cadastro Nacional de Pessoa Jurídica") is valid. /// </summary> /// <param name="_cnpj">The CNPJ number as a string.</param> /// <returns>'true' if the CNPJ is valid, 'false' otherwise.</returns> public static bool IsValidCNPJ(string _cnpj) { if (!string.IsNullOrEmpty(_cnpj)) { int sum = 0; int rest = 0; string axCnpj = string.Empty; string digits = string.Empty; int[] factors1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; int[] factors2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; _cnpj = Format.AsDigitsOnly(_cnpj); if (_cnpj.Length != 14) { return(false); } axCnpj = _cnpj.Substring(0, 12); for (int i = 0; i < 12; i++) { sum += int.Parse(axCnpj[i].ToString()) * factors1[i]; } rest = sum % 11; rest = (rest >= 2) ? 11 - rest : 0; sum = 0; digits += rest; axCnpj += digits; for (int i = 0; i < 13; i++) { sum += int.Parse(axCnpj[i].ToString()) * factors2[i]; } rest = sum % 11; rest = (rest >= 2) ? 11 - rest : 0; digits += rest; return(_cnpj.EndsWith(digits)); } return(false); }
/// <summary> /// Requests info from ViaCEP database, using the given zip code, as a JSON object. /// </summary> /// <param name="_zipcode">The 8-digit zip code ("código CEP") value.</param> /// <returns>A CEPInfo object with the query results.</returns> public static CEP ByZipCode(string _zipcode) { try { if (_zipcode != null) { CEP cep = null; string viaCEPUrl = string.Empty; string webClient = string.Empty; viaCEPUrl += "https://viacep.com.br/ws/"; viaCEPUrl += Uri.EscapeUriString(Format.AsDigitsOnly(_zipcode)); viaCEPUrl += "/json/unicode/"; webClient = new WebClient().DownloadString(viaCEPUrl); cep = JsonConvert.DeserializeObject <CEP>(webClient); if (string.IsNullOrEmpty(cep.Error)) { return(cep); } else { throw new CEPException(CEPException.CEP_NOT_FOUND); } } else { throw new CEPException(CEPException.CEP_IS_NULL); } } catch (CEPException ex) { throw ex; } catch (Exception ex) { throw new CEPException(CEPException.CEP_EXCEPTION, ex); } }
public void AsDigitsOnly_AreEqual() { KeyValuePair <string, string>[] dataset = { new KeyValuePair <string, string>("123456789012345", "123456789012345"), // All numbers new KeyValuePair <string, string>("484.53081.92-9", "48453081929"), // PIS/PASEP new KeyValuePair <string, string>("586.679.570-11", "58667957011"), // CPF new KeyValuePair <string, string>("10.148.817-8", "101488178"), // RG new KeyValuePair <string, string>("19360-000", "19360000"), // CEP new KeyValuePair <string, string>("CXI-4208", "4208"), // Vehicle plate new KeyValuePair <string, string>("@u?1e\nLEbj", "1"), // With letetrs, special chars and escapes new KeyValuePair <string, string>("!1@2#3$4%5{{6", "123456"), // Garbage new KeyValuePair <string, string>("ABCDE", string.Empty), // Only text new KeyValuePair <string, string>("\n", string.Empty), // Escapes new KeyValuePair <string, string>("", string.Empty), // Empty text new KeyValuePair <string, string>("1", "1"), // Single number new KeyValuePair <string, string>("A", string.Empty), // Only numbers new KeyValuePair <string, string>("?", string.Empty), // Special characters new KeyValuePair <string, string>(string.Empty, string.Empty), // Empty text new KeyValuePair <string, string>(Environment.NewLine, string.Empty) // Special characters }; foreach (var data in dataset) { var newLine = Environment.NewLine; var result = Format.AsDigitsOnly(data.Key); var message = string.Format( newLine + "-----------------------------------" + newLine + "| Expected for [" + data.Key + "] --> [" + data.Value + "]." + newLine + "| Obtained for [" + data.Key + "] --> [" + result + "]." + newLine + "-----------------------------------" ); Assert.AreEqual(result, data.Value, message); } }
/// <summary> /// Verifies if two instances of a CEP are equivalent. This method and the operator == are equivalent. /// </summary> /// <param name="obj">The CEP object to compare.</param> /// <returns>'true' if the instances are equivalent, 'false' otherwise.</returns> public override bool Equals(object obj) { CEP cep = (CEP)obj; if (!string.IsNullOrEmpty(ZipCode) && !string.IsNullOrEmpty(cep.ZipCode)) { if (Format.AsDigitsOnly(ZipCode) != Format.AsDigitsOnly(cep.ZipCode)) { return(false); } } if (!string.IsNullOrEmpty(Address) && !string.IsNullOrEmpty(cep.Address)) { StringBuilder addressA = new StringBuilder(Address.ToLower()); StringBuilder addressB = new StringBuilder(cep.Address.ToLower()); addressA.Replace("alameda ", string.Empty); addressB.Replace("alameda ", string.Empty); addressA.Replace("avenida ", string.Empty); addressB.Replace("avenida ", string.Empty); addressA.Replace("rua ", string.Empty); addressB.Replace("rua ", string.Empty); addressA.Replace("travessa ", string.Empty); addressB.Replace("travessa ", string.Empty); if (addressA.ToString() != addressB.ToString()) { return(false); } } if (!string.IsNullOrEmpty(Complement) && !string.IsNullOrEmpty(cep.Complement)) { if (Complement != cep.Complement) { return(false); } } if (!string.IsNullOrEmpty(Neighborhood) && !string.IsNullOrEmpty(cep.Neighborhood)) { StringBuilder neighborhoodA = new StringBuilder(Neighborhood.ToLower()); StringBuilder neighborhoodB = new StringBuilder(cep.Neighborhood.ToLower()); neighborhoodA.Replace("bairro ", string.Empty); neighborhoodB.Replace("bairro ", string.Empty); neighborhoodA.Replace("jardim ", string.Empty); neighborhoodB.Replace("jardim ", string.Empty); neighborhoodA.Replace("residencial ", string.Empty); neighborhoodB.Replace("residencial ", string.Empty); neighborhoodA.Replace("vila ", string.Empty); neighborhoodB.Replace("vila ", string.Empty); if (neighborhoodA.ToString() != neighborhoodB.ToString()) { return(false); } } if (!string.IsNullOrEmpty(City) && !string.IsNullOrEmpty(cep.City)) { if (City.ToLower() != cep.City.ToLower()) { return(false); } } if (!string.IsNullOrEmpty(State) && !string.IsNullOrEmpty(cep.State)) { if (State.ToLower() != cep.State.ToLower()) { return(false); } } if (!string.IsNullOrEmpty(Unity) && !string.IsNullOrEmpty(cep.Unity)) { if (Unity.ToLower() != cep.Unity.ToLower()) { return(false); } } if (!string.IsNullOrEmpty(IBGE) && !string.IsNullOrEmpty(cep.IBGE)) { if (IBGE != cep.IBGE) { return(false); } } if (!string.IsNullOrEmpty(GIA) && !string.IsNullOrEmpty(cep.GIA)) { if (GIA != cep.GIA) { return(false); } } return(true); }