Пример #1
0
        static void Main(string[] args)
        {
            string itemToTest = "";
            string result     = "";

            while (true)
            {
                Console.WriteLine("Insert String to test");
                itemToTest = Console.ReadLine();
                //1
                //Console.WriteLine("result=" + TRValidations.TRCapitalize(itemToTest));

                //2

                /*
                 * if (TRValidations.TRPostalCodeValidation(ref itemToTest))
                 * {
                 *  Console.WriteLine(itemToTest);
                 * }
                 * else
                 *  Console.WriteLine("Failed");
                 */


                //3
                if (TRValidations.TRZipCodeValidation(ref itemToTest))
                {
                    Console.WriteLine(itemToTest);
                }
                else
                {
                    Console.WriteLine("Failed");
                }
            }
        }
Пример #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            Name    = TRValidations.TRCapitalize(Name);
            Address = TRValidations.TRCapitalize(Address);
            Town    = TRValidations.TRCapitalize(Town);
            County  = TRValidations.TRCapitalize(County);

            ProvinceCode = ProvinceCode.ToUpper();
            if (Directions != null)
            {
                Directions = Directions.Trim();
            }

            var province = _context.Province.Where(pr => pr.ProvinceCode == ProvinceCode).FirstOrDefault();
            var country  = province.CountryCode;

            if (Town == null && County == null)
            {
                yield return(new ValidationResult("Either Town or County must be provided!", new[] { nameof(Town), nameof(County) }));
            }

            if (Email == null && (Address == null || PostalCode == null))
            {
                yield return(new ValidationResult("Both Postal code and Address or Email must be provided", new[] { nameof(Email), nameof(Address), nameof(PostalCode) }));
            }

            if (HomePhone == null && CellPhone == null)
            {
                yield return(new ValidationResult("Either Home Phone or Cell Phone must be provided!", new[] { nameof(HomePhone), nameof(CellPhone) }));
            }

            if (DateJoined != null && LastContactDate != null)
            {
                if (DateTime.Compare((DateTime)DateJoined, (DateTime)LastContactDate) == 1)
                {
                    yield return(new ValidationResult("Last Contact Date can not be earlier than Date Joined!", new[] { nameof(DateJoined), nameof(LastContactDate) }));
                }
            }

            var homePhone = HomePhone;

            if (TRValidations.PhoneValidation(ref homePhone))
            {
                HomePhone = homePhone;
            }
            else
            {
                yield return(new ValidationResult("Home Phone must be 10 digits!", new[] { nameof(HomePhone) }));
            }

            var cellPhone = CellPhone;

            if (TRValidations.PhoneValidation(ref cellPhone))
            {
                CellPhone = cellPhone;
            }
            else
            {
                yield return(new ValidationResult("Cell Phone must be 10 digits!", new[] { nameof(CellPhone) }));
            }

            if (PostalCode != null)
            {
                var postalCode = PostalCode;
                switch (country)
                {
                case "US":
                    if (TRValidations.TRZipCodeValidation(ref postalCode))
                    {
                        PostalCode = postalCode;
                    }
                    else
                    {
                        yield return(new ValidationResult("Postal Code is not valid USA zip code!", new[] { nameof(PostalCode) }));
                    }
                    break;

                case "CA":
                    if (TRValidations.TRPostalCodeValidation(ref postalCode))
                    {
                        PostalCode = postalCode;
                    }
                    else
                    {
                        yield return(new ValidationResult("Postal Code is not valid Canadian!", new[] { nameof(PostalCode) }));
                    }
                    break;

                default:
                    yield return(new ValidationResult("Postal Code is not implemented", new[] { nameof(PostalCode) }));

                    break;
                }
            }

            yield return(ValidationResult.Success);
        }