예제 #1
0
        /// <summary>
        /// Determine whether input is a valid IP address of the address families
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IPAddress(this ValidatableValue <string> input, params System.Net.Sockets.AddressFamily[] family)
        {
            if (family == null || !family.Any())
            {
                input.AddError("No AddressFamily specified");
                return(false);
            }

            IPAddress address;

            if (System.Net.IPAddress.TryParse(input.Value, out address))
            {
                if (family.Contains(address.AddressFamily))
                {
                    return(true);
                }

                input.AddError("AddressFamily doesn't match");
            }
            else
            {
                input.AddError("Failed to parse IP address");
            }

            return(false);
        }
예제 #2
0
        /// <summary>
        /// Indicates whether input is in correct format for a credit card number and is valid.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool CreditCard(this ValidatableValue <string> inputV)
        {
            var input = inputV.Value.Replace(" ", "");

            input = input.Replace("-", "");
            if (!input.IsValid().Numeric())
            {
                inputV.AddError("Contains invalid characters");
            }
            else
            {
                var sumOfDigits = 0;
                var pos         = 0;
                for (var i = input.Length - 1; i >= 0; i--)
                {
                    var e = input[i];
                    if (e >= '0' && e <= '9')
                    {
                        var v = ((int)e - 48) * (pos % 2 == 0 ? 1 : 2);

                        sumOfDigits += v / 10 + v % 10;

                        pos++;
                    }
                }

                if (sumOfDigits % 10 != 0)
                {
                    inputV.AddError("Check sum not valid");
                }
            }

            return(inputV.IsValid);
        }
예제 #3
0
        /// <summary>
        /// Indicates whether supplied input is either in ISBN-10 digit format or ISBN-13 digit format.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="version">Valid options are: IsbnVersion.Ten, IsbnVersion.Thirteen or IsbnVersion.Any</param>
        /// <returns></returns>
        /// IsbnVersion
        public static bool Isbn(this ValidatableValue <string> inputVal, IsbnVersion version = IsbnVersion.Any)
        {
            var input = RemoveSpacesAndHyphens(inputVal.Value);

            switch (version)
            {
            case IsbnVersion.Any:
                if (!IsIsbn10(input) && !IsIsbn13(input))
                {
                    inputVal.AddError("Not ISBN 10 or 13");
                }
                return(inputVal.IsValid);

            case IsbnVersion.Thirteen:
                if (!IsIsbn13(input))
                {
                    inputVal.AddError("Not ISBN 13");
                }
                return(inputVal.IsValid);

            case IsbnVersion.Ten:
                if (!IsIsbn10(input))
                {
                    inputVal.AddError("Not ISBN 10");
                }
                return(inputVal.IsValid);
            }
            throw new ArgumentOutOfRangeException(
                      "version",
                      string.Format("Isbn version {0} is not supported.", version));
        }
예제 #4
0
        /// <summary>
        /// Banks the account.
        /// </summary>
        /// <param name="branchNumber">The combind branch/bank number. I.E. UK sort code</param>
        /// <returns></returns>
        public static bool BankAccount(this ValidatableValue <string> inputV, string branchNumber)
        {
            var errors = new List <ValidationResult>();

            foreach (var l in inputV.Locale)
            {
                var validator = new ValidatableValue <string>(inputV.Value, l);

                if (validatorsWithBranch.ContainsKey(l))
                {
                    if (validatorsWithBranch[l](validator, branchNumber))
                    {
                        //is valid no errors
                        return(true);
                    }
                }
                else
                {
                    validator.AddError($"Unable to validate banks for '{l}'");
                }
                errors.AddRange(validator.Errors);
            }

            //lets add all the validation errors (if invalid to the source validatable)
            foreach (var r in errors)
            {
                inputV.AddError(r);
            }

            return(inputV.IsValid);
        }
예제 #5
0
 /// <summary>
 /// Determines whether the given phone number is a mobile phone number or not. Based on the current locale of the executing thread
 /// </summary>
 /// <param name="phoneNumber">The phone number to check.</param>
 /// <param name="exitCode">The exit code.</param>
 /// <returns>
 /// True if it is a mobile phone number, false otherwise.
 /// </returns>
 /// <remarks>
 /// Relies on locales that use specific blocks of numbers for mobile phone numbers.
 /// </remarks>
 public static bool MobilePhone(this ValidatableValue <string> phoneNumber, string exitCode)
 {
     if (!phoneNumber.Locale.Any(loc => IsLocalPhone(phoneNumber, loc, exitCode)))
     {
         phoneNumber.AddError("Not a recognised mobile phone number");
     }
     return(phoneNumber.IsValid);
 }
예제 #6
0
        private static bool IsLocalPhone(ValidatableValue <string> phoneNumber, string locale, string exitCode)
        {
            if (!phoneNumber.IsValueSet || string.IsNullOrWhiteSpace(phoneNumber.Value))
            {
                return(false);
            }

            Validator validator;

            if (LocaleMobilePhoneRegexes.TryGetValue(locale, out validator))
            {
                return(validator.Validate(phoneNumber.Value, exitCode));
            }
            return(false);
        }
예제 #7
0
파일: IsIban.cs 프로젝트: olivif/IsValid
        /// <summary>
        /// Indicates whether supplied input is either in ISBN-10 digit format or ISBN-13 digit format.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="version">Valid options are: IsbnVersion.Ten, IsbnVersion.Thirteen or IsbnVersion.Any</param>
        /// <returns></returns>
        /// IsbnVersion
        public static bool Iban(this ValidatableValue <string> inputVal)
        {
            var val         = inputVal.Value.ToUpper();
            var countryCode = val.Trim().Substring(0, 2);

            var validator = _countryValidationLength.FirstOrDefault(x => x.CountryCode == countryCode);

            if (validator == null)
            {
                inputVal.AddError("Unrecognised country code");
                return(false);
            }

            return(validator.Validate(inputVal));
        }
예제 #8
0
        /// <summary>
        /// Determine whether input is in lower case.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Uppercase(this ValidatableValue <string> input)
        {
            if (!input.IsValueSet || input.Value == null)
            {
                return(true);
            }

            var val = input.Value;

            if (val != val.ToUpper())
            {
                input.AddError("Contains lowercase characters");
            }

            return(input.IsValid);
        }
예제 #9
0
        public static bool Model(this ValidatableValue <object> input)
        {
            if (input.Value == null)
            {
                return(false);
            }

            var context = new ValidationContext(input.Value);
            //context.MemberName = "Range";
            var results = new List <ValidationResult>();

            System.ComponentModel.DataAnnotations.Validator.TryValidateObject(input.Value, context, results, true);

            input.AddError(results);

            return(input.IsValid);
        }
예제 #10
0
        /// <summary>
        /// Indicates whether supplied input is either in ISBN-10 digit format or ISBN-13 digit format.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="version">Valid options are: IsbnVersion.Ten, IsbnVersion.Thirteen or IsbnVersion.Any</param>
        /// <returns></returns>
        /// IsbnVersion
        public static bool BusinessIdentifierCode(this ValidatableValue <string> inputVal)
        {
            var val = inputVal.Value;

            if (!(val.Length == 8 || val.Length == 11))
            {
                inputVal.AddError("Invalid length");
            }
            if (val.Length >= 4)
            {
                var institutionCode = inputVal.Value.Substring(0, 4);
                if (institutionCode.Any(x => !Char.IsLetter(x)))
                {
                    //must be made up of letters
                    inputVal.AddError("Invalid Institution Code");
                }
            }

            if (val.Length >= 6)
            {
                var countryCode = inputVal.Value.Substring(4, 2).ToUpperInvariant();
                if (!Constants.CountryCodes.Codes.Contains(countryCode))
                {
                    inputVal.AddError("Invalid Country Code");
                }
            }
            if (val.Length >= 8)
            {
                var locationCode = inputVal.Value.Substring(6, 2).ToUpperInvariant();
                if (locationCode.Any(x => !Char.IsLetterOrDigit(x)))
                {
                    //must be made up of letters
                    inputVal.AddError("Invalid Location Code");
                }
            }
            if (val.Length >= 11)
            {
                var branchCode = inputVal.Value.Substring(8, 3).ToUpperInvariant();
                if (branchCode.Any(x => !Char.IsLetterOrDigit(x)))
                {
                    //must be made up of letters
                    inputVal.AddError("Invalid Branch Code");
                }
            }
            return(inputVal.IsValid);
        }
예제 #11
0
파일: IsEmail.cs 프로젝트: olivif/IsValid
        /// <summary>
        /// Determine whether input matches a valid email address.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Email(this ValidatableValue <string> input)
        {
            try
            {
                if (new MailAddress(input.Value).Address != input.Value)
                {
                    input.AddError("Input doesn't match address part");
                }

                return(true);
            }
            catch (Exception ex)
            {
                input.AddError(ex.Message);
            }


            return(false);
        }
예제 #12
0
        /// <summary>
        /// Determine whether input matches a valid email address.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Email(this ValidatableValue <string> input)
        {
            try
            {
                var validationResult = IsValid(input.Value);
                if (validationResult != null)
                {
                    input.AddError(validationResult);
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                input.AddError(ex.Message);
            }


            return(false);
        }
예제 #13
0
파일: IsNumeric.cs 프로젝트: olivif/IsValid
        /// <summary>
        /// Indicates whether input is in correct format for a number and is valid.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool Numeric(this ValidatableValue <string> inputValue)
        {
            var input = inputValue.Value;

            if (input == null)
            {
                inputValue.AddError("Null value");
                return(false);
            }
            int length = input.Length;

            if (length == 0)
            {
                inputValue.AddError("No characters");
                return(false);
            }
            int i = 0;

            if (input[0] == '-')
            {
                if (length == 1)
                {
                    inputValue.AddError("Contains only '-'");
                    return(false);
                }
                i = 1;
            }
            for (; i < length; i++)
            {
                char c = input[i];
                if (c <= '/' || c >= ':')
                {
                    inputValue.AddError("Contains out of range character");
                    return(false);
                }
            }

            return(true);
        }
예제 #14
0
 public static bool BankAccount(this ValidatableValue <string> inputV)
 {
     return(inputV.BankAccount(null));
 }
예제 #15
0
 /// <summary>
 /// Determine whether input is a valid IP address
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static bool IPAddress(this ValidatableValue <string> input)
 {
     throw new NotSupportedException("IPAddress validation not supported this platform");
 }
예제 #16
0
 /// <summary>
 /// Determine whether input is a valid IP address
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static bool IPAddress(this ValidatableValue <string> input)
 {
     return(input.IPAddress(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.AddressFamily.InterNetworkV6));
 }
예제 #17
0
파일: IsIban.cs 프로젝트: olivif/IsValid
            internal bool Validate(ValidatableValue <string> inputVal)
            {
                var val       = inputVal.Value.ToUpper();
                var charCount = val.Count(Char.IsLetterOrDigit);

                if (charCount != Length)
                {
                    inputVal.AddError("Invalid length");
                    return(false);
                }

                var sb = new StringBuilder();

                for (var i = 0; i < val.Length; i++)
                {
                    var c = val[i];
                    if (!Char.IsWhiteSpace(c))
                    {
                        if (Char.IsLetter(c))
                        {
                            //letter
                            var v = (Char.ToUpper(c) - 'A') + 10;
                            sb.Append(v);
                        }
                        else if (Char.IsDigit(c))
                        {
                            sb.Append(c);
                        }
                        else
                        {
                            inputVal.AddError("Contains invalid character(s)");
                            return(false);
                        }
                    }
                }

                val = sb.ToString();
                val = val.Substring(6) + val.Substring(0, 6);


                var intVal = BigIntegerHelpers.Parse(val);

                var remainder = System.Numerics.BigInteger.Remainder(intVal, new System.Numerics.BigInteger(97));

                if (!remainder.IsOne)
                {
                    inputVal.AddError("Invalid check digit");
                }

                //lets try and validate the actual account details
                if (!string.IsNullOrWhiteSpace(LocaleCode))
                {
                    val = inputVal.Value.ToUpper().Replace(" ", "");
                    var    m        = Pattern.Match(val);
                    string bankCode = null;
                    if (m.Groups["CG_S"] != null)
                    {
                        bankCode = m.Groups["CG_S"].Value;      //sort code
                    }
                    var accountNumber = m.Groups["CG_C"].Value; //account number
                    var validator     = accountNumber.IsValid(LocaleCode);
                    if (!validator.BankAccount(bankCode))
                    {
                        foreach (var e in validator.Errors)
                        {
                            inputVal.AddError(e);
                        }
                    }
                }

                return(inputVal.IsValid);
            }
예제 #18
0
 /// <summary>
 /// Determines whether the given phone number is a mobile phone number or not. Based on the current locale of the executing thread
 /// </summary>
 /// <param name="phoneNumber">The phone number to check.</param>
 /// <returns>True if it is a mobile phone number, false otherwise.</returns>
 /// <remarks>
 /// Relies on locales that use specific blocks of numbers for mobile phone numbers.
 /// </remarks>
 public static bool MobilePhone(this ValidatableValue <string> phoneNumber)
 {
     return(MobilePhone(phoneNumber, null));
 }