Exemple #1
0
        public bool ValidHeight()
        {
            if (Height == default)
            {
                return(false);
            }

            // Unable to parse
            if (!int.TryParse(Height.Replace("cm", "").Replace("in", ""), out int parsedHeight))
            {
                return(false);
            }

            if (Height.EndsWith("cm"))
            {
                return(parsedHeight >= 150 && parsedHeight <= 193);
            }

            if (Height.EndsWith("in"))
            {
                return(parsedHeight >= 59 && parsedHeight <= 76);
            }

            return(false);
        }
Exemple #2
0
            public bool IsValid()
            {
                var validPasswordIdRegex = new Regex("^[0-9]{9}$", RegexOptions.Compiled);

                string[] validEyeColors      = { "amb", "blu", "brn", "gry", "grn", "hzl", "oth" };
                var      validHairColorRegex = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled);

                if (!IsComplete())
                {
                    return(false);
                }
                if (BirthYear < 1920 || BirthYear > 2002)
                {
                    return(false);
                }
                if (IssueYear < 2010 || IssueYear > 2020)
                {
                    return(false);
                }
                if (ExpirationYear < 2020 || ExpirationYear > 2030)
                {
                    return(false);
                }
                if (!validPasswordIdRegex.IsMatch(PasswordId))
                {
                    return(false);
                }
                if (!validEyeColors.Contains(EyeColor))
                {
                    return(false);
                }
                if (!validHairColorRegex.IsMatch(HairColor))
                {
                    return(false);
                }

                if (!Height.EndsWith("cm") && !Height.EndsWith("in"))
                {
                    return(false);
                }

                var heightNumber = Convert.ToInt32(Height.Substring(0, Height.Length - 2));

                if (Height.EndsWith("in") && (heightNumber < 59 || heightNumber > 76))
                {
                    return(false);
                }
                if (Height.EndsWith("cm") && (heightNumber < 150 || heightNumber > 193))
                {
                    return(false);
                }

                return(true);
            }
Exemple #3
0
 public bool HeightValid()
 {
     if (Height.EndsWith("cm"))
     {
         var cms = Convert.ToInt32(Height.Substring(0, Height.Length - 2));
         return(cms >= 150 && cms <= 193);
     }
     else if (Height.EndsWith("in"))
     {
         var ins = Convert.ToInt32(Height.Substring(0, Height.Length - 2));
         return(ins >= 59 && ins <= 76);
     }
     return(false);
 }
        private bool IsHeightValid()
        {
            if (Height.EndsWith("cm"))
            {
                var height = int.Parse(Height.Substring(0, Height.Length - 2));
                return(height >= 150 && height <= 193);
            }

            if (Height.EndsWith("in"))
            {
                var height = int.Parse(Height.Substring(0, Height.Length - 2));
                return(height >= 59 && height <= 76);
            }

            return(false);
        }
Exemple #5
0
        private bool ValidHeight()
        {
            int heightValue;

            if (Height.EndsWith("in"))
            {
                if (!int.TryParse(Height.Replace("in", ""), out heightValue))
                {
                    return(false);
                }
                return(59 <= heightValue && heightValue <= 76);
            }
            if (Height.EndsWith("cm"))
            {
                if (!int.TryParse(Height.Replace("cm", ""), out heightValue))
                {
                    return(false);
                }
                return(150 <= heightValue && heightValue <= 193);
            }
            return(false);
        }
Exemple #6
0
 public bool IsValidHeight(bool strictSecurity)
 {
     if (Height == null)
     {
         return(false);
     }
     if (strictSecurity)
     {
         if (Height.EndsWith("cm"))
         {
             if (Height.Replace("cm", "").ToInt() < 150)
             {
                 return(false);
             }
             if (Height.Replace("cm", "").ToInt() > 193)
             {
                 return(false);
             }
         }
         else if (Height.EndsWith("in"))
         {
             if (Height.Replace("in", "").ToInt() < 59)
             {
                 return(false);
             }
             if (Height.Replace("in", "").ToInt() > 76)
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
        public bool IsValid()
        {
            // check if all data present
            if (!(
                    BirthYear != null &&
                    IssueYear != null &&
                    ExpirationYear != null &&
                    Height != null &&
                    HairColor != null &&
                    EyeColor != null &&
                    PassportId != null))
            {
                return(false);
            }

            // validate birth year
            if (BirthYear < 1920 || BirthYear > 2002)
            {
                return(false);
            }

            // validate issue year
            if (IssueYear < 2010 || IssueYear > 2020)
            {
                return(false);
            }

            // validate expiration year
            if (ExpirationYear < 2020 || ExpirationYear > 2030)
            {
                return(false);
            }

            // validate height
            if (!Height.EndsWith("cm") && !Height.EndsWith("in"))
            {
                return(false);
            }

            try
            {
                int    heightNumber = Convert.ToInt32(Height.Substring(0, Height.Length - 2));
                string heightType   = Height.Substring(Height.Length - 2);
                if (heightType == "cm" && (heightNumber < 150 || heightNumber > 193))
                {
                    return(false);
                }
                if (heightType == "in" && (heightNumber < 59 || heightNumber > 76))
                {
                    return(false);
                }
            } catch
            {
                // Cannot parse
                throw new Exception("Cannot parse " + Height);
            }

            // validate hair color
            if (HairColor.Length != 7 || !HairColor.StartsWith("#"))
            {
                return(false);
            }
            var allowedChars = new List <char> {
                '#', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
            };

            for (var i = 0; i < HairColor.Length; i++)
            {
                var currentChar = Convert.ToChar(HairColor.Substring(i, 1));
                if (!allowedChars.Contains(currentChar))
                {
                    return(false);
                }
            }

            // validate eye color
            var allowedValues = new List <string> {
                "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
            };

            if (!allowedValues.Contains(EyeColor))
            {
                return(false);
            }

            // validate passport number
            if (PassportId.Length != 9)
            {
                return(false);
            }
            allowedChars = new List <char> {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
            for (var i = 0; i < PassportId.Length; i++)
            {
                var currentChar = Convert.ToChar(PassportId.Substring(i, 1));
                if (!allowedChars.Contains(currentChar))
                {
                    return(false);
                }
            }

            // Passed all validations - return true;
            return(true);
        }
Exemple #8
0
        public bool Validate(bool validateContent)
        {
            // Country ID deliberately skipped
            if (BirthYear == null || IssueYear == null || ExpiryYear == null || Height == null ||
                HairColorHex == null || EyeColorHex == null || PassportId == null)
            {
                return(false);
            }

            if (!validateContent)
            {
                return(true);
            }

            if (BirthYear < 1920 || BirthYear > 2002)
            {
                return(false);
            }

            if (IssueYear < 2010 || IssueYear > 2020)
            {
                return(false);
            }

            if (ExpiryYear < 2020 || ExpiryYear > 2030)
            {
                return(false);
            }

            var heightNum = Convert.ToInt16(Height.Substring(0, Height.Length - 2));

            if (Height.EndsWith("in"))
            {
                if (heightNum < 59 || heightNum > 76)
                {
                    return(false);
                }
            }
            else if (Height.EndsWith("cm"))
            {
                if (heightNum < 150 || heightNum > 193)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            if (!Regex.IsMatch(HairColorHex, @"\A#[\da-f]{6}\z"))
            {
                return(false);
            }

            if (!(new[] { "amb", "blu", "brn", "gry", "grn", "hzl", "oth" }).Contains(EyeColorHex))
            {
                return(false);
            }

            if (!Regex.IsMatch(PassportId, @"\A\d{9}\z"))
            {
                return(false);
            }

            return(true);
        }