Roberto Sánchez - Programador/Desarrollador .NET

Recursos para programadores/desarrolladores C++.

Validation.dll

Bibloteca estática de clases sin constructor, con tres métodos para el cálculo de validación de correo electrónico, número de CIF y número de IBAN.

Los tres métodos devuelven 'true' o 'false' según el resultado de la validación.

using namespace System;
using namespace System::Text;
using namespace System::Text::RegularExpressions;
namespace RSoftware
{
    namespace Validation
    {
        bool ValidationRules::ValidateNumberCIF(String ^numberCIF)
        {
            if (String::IsNullOrEmpty(numberCIF) == true)
                return false;
            numberCIF = numberCIF->Trim()->ToUpper();
            if (numberCIF->Length != 9)
                return false;
            String ^firstChar = numberCIF->Substring(0, 1);
            String ^firstCharacterInterval = "ABCDEFGHJNPQRSUVW";
            if (firstCharacterInterval->IndexOf(firstChar) == -1)
                return false;
            try
            {
                int pairSum = default(int);
                int oddSum = default(int);
                String ^numberCIFWithoutCheckDigit = numberCIF->Substring(0, 8);
                String ^digits = numberCIFWithoutCheckDigit->Substring(1, 7);
                for (int n = 0; n <= digits->Length - 1; n += 2)
                {
                    if (n < 6)
                        pairSum = Convert::ToInt32(digits[n + 1].ToString());
                    int doubleOdd = 2 * Convert::ToInt32(digits[n].ToString());
                    oddSum += (doubleOdd % 10) + (doubleOdd / 10);
                }
                int totalSum = pairSum + oddSum;
                totalSum = (10 - (totalSum % 10)) % 10;
                String ^checkDigit = String::Empty;
                if (firstChar == "N" || firstChar == "P" || firstChar == "Q" || firstChar == "R" || firstChar == "S" || firstChar == "W" )
                {
                    array<Char> ^characters = { 'J', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
                    checkDigit = characters[totalSum].ToString();
                }
                else
                    checkDigit = totalSum.ToString();
                return checkDigit->Equals(numberCIF-›Substring(8, 1);
            }
            catch (...)
            {
                return false;
            }
        }
        bool ValidationRules::ValidateEmail(String ^email)
        {
            if (String::IsNullOrEmpty(email) == true)
                return false;
            String ^format = R"(\w+([-+.']\w+)*\w+([-.]\w+)*\.\w+([-.]\w+)*)";
            if (Regex::IsMatch(email, format))
            {
                if (Regex::Replace(email, format, String::Empty)->Length == 0)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        bool ValidationRules::ValidateNumberIBAN(String ^numberIBAN)
        {
            if (String::IsNullOrEmpty(numberIBAN) == true)
                return false;
            else if (Regex::IsMatch(numberIBAN, "^[A-Z0-9]"))
            {
                String ^bank = numberIBAN->Substring(4, numberIBAN->Length - 4) + numberIBAN->Substring(0, 4);
                int asciiShift = 55;
                auto stringBuilder = gcnew StringBuilder();
                for each (Char character in bank)
                {
                    int variable;
                    if (Char::IsLetter(character))
                        variable = character - asciiShift;
                    else
                        variable = int::Parse(character.ToString());
                    stringBuilder->Append(variable);
                }
                String ^checksumString = stringBuilder->ToString();
                int checksum = int::Parse(checksumString->Substring(0, 1));
                for (int counter = 1; counter < checksumString->Length; counter++)
                {
                    int variable = int::Parse(checksumString->Substring(counter, 1));
                    checksum *= 10;
                    checksum += variable;
                    checksum %= 97;
                }
                return checksum == 1;
            }
            else
                return false;
        }
    }
}