Roberto Sánchez - Programador/Desarrollador aplicaciones .NET

Recursos para programadores/desarrolladores C#.

Maths.dll

Biblioteca de tipos para cálculo matemático.

- Números complejos.

- Números racionales.

- Matrices de 2, 3 y 4 dimensiones.

- Vectores de 2, 3 y 4 dimensiones.

- Cuaternios.

- Geometría en 2 y 3 dimensiones.

Biblioteca en constante desarrollo. Si deseas participar en el desarrollo, ponte en contacto conmigo.

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.

Ejemplos de uso:

Validation.ValidationRules.ValidateEmail("email@email.com");

Validation.ValidationRules.ValidateNumberCIF("A123456789");

Validation.ValidationRules.ValidateNumberIBAN("ES0011112222331234567890");

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

using System;
using System.Text;
using System.Text.RegularExpressions;
namespace RSoftware.Validation
{
    /// ‹summary›
    /// Validation rules.
    /// ‹/summary›
    public static class ValidationRules
    {
        /// ‹summary›
        /// Validates CIF numer.
        /// ‹/summary›
        /// ‹param name="numberCIF"›The CIF number to validate.‹/param›
        /// ‹returns›
        /// ‹para›Returns 'true' if the validation is correct.‹/para›
        /// ‹para›Returns 'false' if the validation is not correct.‹/para›
        /// ‹/returns›
        public static bool 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;
                switch (firstChar)
                {
                    case "N":
                    case "P":
                    case "Q":
                    case "R":
                    case "S":
                    case "W":
                        char[] characters = { 'J', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
                        checkDigit = characters[totalSum].ToString();
                        break;
                    default:
                        checkDigit = totalSum.ToString();
                        break;
                }
                return checkDigit.Equals(numberCIF.Substring(8, 1));
            }
            catch
            {
                return false;
            }
        }
        /// ‹summary›
        /// Validates email format.
        /// ‹/summary›
        /// ‹param name="email"›The email to validate.‹/param›
        /// ‹returns›
        /// ‹para›Returns 'true' if the validation is correct.‹/para›
        /// ‹para›Returns 'false' if the validation is not correct.‹/para›
        /// ‹/returns›
        public static bool ValidateEmail(string email)
        {
            if (string.IsNullOrEmpty(email) == true)
                return false;
            string format = @"\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;
        }
        /// ‹summary›
        /// Validates IBAN number.
        /// ‹/summary›
        /// ‹param name="numberIBAN"›The IBAN number to validate.‹/param›
        /// ‹returns›
        /// ‹para›Returns 'true' if the validation is correct.‹/para›
        /// ‹para›Returns 'false' if the validation is not correct.‹/para›
        /// ‹/returns›
        public static bool ValidateNumberIBAN(string numberIBAN)
        {
            if (string.IsNullOrEmpty(numberIBAN))
                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;
                    var stringBuilder = new StringBuilder();
                    foreach (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;
            }
        }
    }
}