コード例 #1
0
        public override void ValidationCheck(List <string> _errors)
        {
            if (!string.IsNullOrEmpty(Recipient) && (Recipient.Trim().Length < 2 || Recipient.Trim().Length > 40))
            {
                _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidRecipentName"));
            }

            if (!string.IsNullOrEmpty(ZipCode))
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(ZipCode, @"^(\d{6})$"))
                {
                    _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidZipCode"));
                }
            }

            if (!string.IsNullOrEmpty(StateProvince) && (StateProvince.Trim().Length < 1 || StateProvince.Trim().Length > 30))
            {
                _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidState"));
            }

            if (!string.IsNullOrEmpty(City) && (City.Trim().Length < 1 || City.Trim().Length > 20))
            {
                _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidCity"));
            }

            if (!string.IsNullOrEmpty(StreetAddress) && (StreetAddress.Trim().Length < 1 || StreetAddress.Trim().Length > 60))
            {
                _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidStreetAddress"));
            }

            if (!string.IsNullOrEmpty(PhoneNumber))
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(_shippingAddr.Phone, @"^(\d{10,15})$"))
                {
                    _errors.Add(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidPhone"));
                }
            }
        }
コード例 #2
0
ファイル: Address.cs プロジェクト: secc/RockPlugins
        public Dictionary <string, string> Validate()
        {
            Dictionary <string, string> ValErrors = new Dictionary <string, string>();

            if (String.IsNullOrEmpty(StreetAddress.Trim()))
            {
                ValErrors.Add("Street Address", "Street Address is required.");
            }
            if (String.IsNullOrEmpty(City.Trim()))
            {
                ValErrors.Add("City", "City is required.");
            }
            if (String.IsNullOrEmpty(State.Trim()))
            {
                ValErrors.Add("State", "State is required.");
            }
            if (String.IsNullOrEmpty(PostalCode.Trim()))
            {
                ValErrors.Add("Postal Code", "Postal Code is required.");
            }

            return(ValErrors);
        }
コード例 #3
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            _context = new ClubsContext();  //requires code somewhere else

            //var _context = validationContext.GetService<ClubsContext>(); // requires dependencyinjection

            // all strings that are null become empty and trim all
            if (FirstName == null)
            {
                FirstName = "";
            }
            FirstName = FirstName.Trim();
            if (LastName == null)
            {
                LastName = "";
            }
            LastName = LastName.Trim();
            if (CompanyName == null)
            {
                CompanyName = "";
            }
            CompanyName = CompanyName.Trim();
            if (StreetAddress == null)
            {
                StreetAddress = "";
            }
            StreetAddress = StreetAddress.Trim();
            if (PostalCode == null)
            {
                PostalCode = "";
            }
            PostalCode = PostalCode.Trim();
            if (ProvinceCode == null)
            {
                ProvinceCode = "";
            }
            ProvinceCode = ProvinceCode.Trim();
            if (Email == null)
            {
                Email = "";
            }
            Email = Email.Trim();
            if (Phone == null)
            {
                Phone = "";
            }
            Phone = Phone.Trim();

            // use our prebuilt string manipulators to capitalize and extract digits
            FirstName     = SDStringManipulation.SDCapitalize(FirstName);
            LastName      = SDStringManipulation.SDCapitalize(LastName);
            CompanyName   = SDStringManipulation.SDCapitalize(CompanyName);
            StreetAddress = SDStringManipulation.SDCapitalize(StreetAddress);
            City          = SDStringManipulation.SDCapitalize(City);

            Phone = SDStringManipulation.SDExtractDigits(Phone);

            if (FirstName == "" && LastName == "" && CompanyName == "")
            {
                yield return(new ValidationResult("You must enter either First Name, Last Name, or Company Name."));
            }

            if (ProvinceCode != "")
            {
                var province = _context.Province.Where(a => a.ProvinceCode.ToLower() == ProvinceCode.ToLower()).FirstOrDefault(); // pull province
                if (province == null)
                {
                    yield return(new ValidationResult("The Province Code must be an existing province code within our database. Good luck figuring them out lol", new string[] { nameof(ProvinceCode) }));
                }
                else
                {
                    ProvinceCode = ProvinceCode.ToUpper(); // to save nicely

                    var country = _context.Country.Where(a => a.CountryCode == province.CountryCode).FirstOrDefault();
                    if (PostalCode != "")
                    {
                        if (!SDStringManipulation.SDPostalCodeIsValid(PostalCode.ToUpper(), country.PostalPattern)) // runs the postal through the regex for the country's postal pattern
                        {
                            yield return(new ValidationResult("The postal code must match the country's format", new string[] { nameof(PostalCode) }));
                        }

                        if (country.Name == "Canada")
                        {
                            PostalCode = PostalCode.ToUpper(); // you would be ashamed of how long I was stuck here before realizing the regex was case sensitive (and that we should be storing them as capitals anyway)
                            if (PostalCode != "")
                            {
                                char firstLetter = PostalCode[0];
                                if (!province.FirstPostalLetter.Contains(firstLetter)) // checks against the saved first letters for province postal codes
                                {
                                    yield return(new ValidationResult("The postal code must match the province's postal codes.", new string[] { nameof(PostalCode) }));
                                }
                            }
                        }
                        if (PostalCode.Length > 6)
                        {
                            PostalCode.Insert(3, " ");
                        }
                    }
                }
            }

            if (Email == "")
            {
                if (StreetAddress == "" || City == "" || PostalCode == "" || ProvinceCode == "")
                {
                    yield return(new ValidationResult("You must submit either a valid email or all postal information."));
                }
            }

            Regex regPhone = new Regex(@"^[0-9]{10}$"); // upon coming back I realize I basically did this in my string manipulation and just have to check length

            // but hey it tests length too and I am just super done with this :)
            if (Phone == "")
            {
                yield return(new ValidationResult("Please enter a valid phone number.", new string[] { nameof(Phone) }));
            }
            else if (!regPhone.IsMatch(Phone))
            {
                yield return(new ValidationResult("Please enter a 10 digit phone number.", new string[] { nameof(Phone) }));
            }
            else
            {
                Phone = Phone.Insert(3, "-");
                Phone = Phone.Insert(7, "-");
            }

            yield return(ValidationResult.Success);
        }