public void TrimAll() { FacilityNumber = FacilityNumber?.Trim(); Name = Name?.Trim(); FileLabel = FileLabel?.Trim(); Location = Location?.Trim(); Address = Address?.Trim(); City = City?.Trim(); PostalCode = PostalCode?.Trim(); }
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); }
/// <summary> /// Allows you to compare another address object to determine if the two addresses are the same. /// </summary> /// <param name="a2">Another address object.</param> /// <returns>If true, the current address matches the address in the parameter.</returns> public bool IsEqualTo(Address a2) { if (a2 == null) { return(false); } var result = true; if (string.Compare(NickName.Trim(), a2.NickName.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(FirstName.Trim(), a2.FirstName.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(MiddleInitial.Trim(), a2.MiddleInitial.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(LastName.Trim(), a2.LastName.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Company.Trim(), a2.Company.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Line1.Trim(), a2.Line1.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Line2.Trim(), a2.Line2.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Line3.Trim(), a2.Line3.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(RegionBvin.Trim(), a2.RegionBvin.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(City.Trim(), a2.City.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(PostalCode.Trim(), a2.PostalCode.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(CountryBvin.Trim(), a2.CountryBvin.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Phone.Trim(), a2.Phone.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(Fax.Trim(), a2.Fax.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } if (string.Compare(WebSiteUrl.Trim(), a2.WebSiteUrl.Trim(), true, CultureInfo.InvariantCulture) != 0) { result = false; } //if (this.Residential != a2.Residential) { // result = false; //} return(result); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { // First Name if (FirstName == null || FirstName.Trim() == "") { yield return(new ValidationResult("First Name cannot be empty.", new[] { nameof(FirstName) })); FirstName = ""; } else { FirstName = NSValidations.NSCapitalize(FirstName.Trim()); } // Last Name if (LastName == null || LastName.Trim() == "") { yield return(new ValidationResult("Last Name cannot be empty.", new[] { nameof(LastName) })); LastName = ""; } else { LastName = NSValidations.NSCapitalize(LastName.Trim()); } // Capitalization SpouseFirstName = NSValidations.NSCapitalize(SpouseFirstName); SpouseLastName = NSValidations.NSCapitalize(SpouseLastName); Street = NSValidations.NSCapitalize(Street); City = NSValidations.NSCapitalize(City); // Full Name if (FirstName.Trim() != "" && LastName.Trim() != "") { FullName = NSValidations.NSFullName(FirstName, LastName, SpouseFirstName, SpouseLastName); } //Access To Context SailContext _context; var optionsBuilder = new DbContextOptionsBuilder <SailContext>(); optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-55PDBKAC\SQLEXPRESSNS;Initial Catalog=Sail;Integrated Security=True"); _context = new SailContext(optionsBuilder.Options); // Province Code Province currentRecord = null; if (!String.IsNullOrEmpty(ProvinceCode)) { bool error = false; try { currentRecord = _context.Province.Where(x => x.ProvinceCode == ProvinceCode).ToArray()[0]; } catch (Exception) { error = true; } if (error) { yield return(new ValidationResult("Province Code must be a valid province.", new[] { nameof(ProvinceCode) })); ProvinceCode = ""; } } // Postal Code if (!String.IsNullOrEmpty(PostalCode) && String.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("To enter a postal code, you must enter a valid province code.", new[] { nameof(PostalCode) })); } else if (!String.IsNullOrEmpty(PostalCode)) { if (currentRecord.CountryCode == "CA" && NSValidations.NSPostalCodeValidation(PostalCode)) { PostalCode = NSValidations.NSPostalCodeFormat(PostalCode); } else if (currentRecord.CountryCode == "US" && NSValidations.NSZipCodeValidation(PostalCode)) { PostalCode = NSValidations.NSZipCodeFormat(PostalCode); } else { yield return(new ValidationResult("Postal Code must be valid.", new[] { nameof(ProvinceCode) })); } } // Home Phone if (String.IsNullOrEmpty(HomePhone) || !NSValidations.TelephoneNumberValidate(HomePhone)) { yield return(new ValidationResult("Phone number must be valid.", new[] { nameof(HomePhone) })); } else { HomePhone = NSValidations.TelephoneNumberFormat(HomePhone); } // Email if (!String.IsNullOrEmpty(Email) && !NSValidations.NSValidEmail(Email)) { yield return(new ValidationResult("Email must be valid.", new[] { nameof(Email) })); } //Year Joined bool isEdit = false; try { Member checkEdit = _context.Member.Where(x => x.MemberId == MemberId).ToArray()[0]; isEdit = true; } catch (Exception) { } if (isEdit) { if (YearJoined != null && !NSValidations.NSYearValidation(YearJoined)) { yield return(new ValidationResult("Year joined cannot be in the future.", new[] { nameof(YearJoined) })); } } else { if (!NSValidations.NSYearValidation(YearJoined)) { yield return(new ValidationResult("Year joined is required and cannot be in the future.", new[] { nameof(YearJoined) })); } } // Canada Post if (!UseCanadaPost && Email == null) { yield return(new ValidationResult("If you will not use Canada Post, you must enter an email.", new[] { nameof(Email), nameof(UseCanadaPost) })); } if (UseCanadaPost) { if (Street == null || Street.Trim() == "") { yield return(new ValidationResult("If you will use Canada Post, you must enter a street.", new[] { nameof(Street), nameof(UseCanadaPost) })); } if (City == null || City.Trim() == "") { yield return(new ValidationResult("If you will use Canada Post, you must enter a city.", new[] { nameof(City), nameof(UseCanadaPost) })); } if (ProvinceCode == null || ProvinceCode.Trim() == "") { yield return(new ValidationResult("If you will use Canada Post, you must enter a Province Code.", new[] { nameof(ProvinceCode), nameof(UseCanadaPost) })); } if (PostalCode == null || PostalCode.Trim() == "") { yield return(new ValidationResult("If you will use Canada Post, you must enter a Postal Code.", new[] { nameof(PostalCode), nameof(UseCanadaPost) })); } } }
//Validate Method public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { OECContext _context = OECContext_Singleton.Context(); //Trim all strings of leading and trailing spaces if (string.IsNullOrEmpty(Name) == false || string.IsNullOrWhiteSpace(Name)) { Name = Name.Trim(); //Use SKValidations.SKCapitalize to capitalize Name Name = SKValidations.SKCapitalize(Name); } if (Name == "") { yield return(new ValidationResult( "Name cannot be an empty string", new string[] { nameof(Name) })); } if (string.IsNullOrEmpty(Address) == false) { Address = Address.Trim(); //Use SKValidations.SKCapitalize to capitalize Address Address = SKValidations.SKCapitalize(Address); } if (string.IsNullOrEmpty(Town) == false) { Town = Town.Trim(); //Use SKValidations.SKCapitalize to capitalize Town Town = SKValidations.SKCapitalize(Town); } if (string.IsNullOrEmpty(County) == false) { County = County.Trim(); //Use SKValidations.SKCapitalize to capitalize County County = SKValidations.SKCapitalize(County); } if (string.IsNullOrEmpty(ProvinceCode) == false) { ProvinceCode = ProvinceCode.Trim(); Regex pattern = new Regex(@"^[a-zA-Z]{2}$"); //Force ProvinceCode to upper before writing to database ProvinceCode = ProvinceCode.ToUpper(); } if (string.IsNullOrEmpty(PostalCode) == false) { PostalCode = PostalCode.Trim(); } if (string.IsNullOrEmpty(HomePhone) == false) { HomePhone = HomePhone.Trim(); } if (string.IsNullOrEmpty(CellPhone) == false) { CellPhone = CellPhone.Trim(); } if (string.IsNullOrEmpty(Email) == false) { Email = Email.Trim(); } if (string.IsNullOrEmpty(Directions) == false) { Directions = Directions.Trim(); } //Either town or county must be provided, both are ok, but not necessary if (string.IsNullOrEmpty(Town) == true && string.IsNullOrEmpty(County) == true) { yield return(new ValidationResult( "At least one of Town or County must be provided.", new string[] { nameof(Town), nameof(County) })); } //If email is not provided, address and postal code must be provided if (string.IsNullOrEmpty(Email) == true && (string.IsNullOrEmpty(Address) == true || string.IsNullOrEmpty(PostalCode) == true)) { yield return(new ValidationResult( "Either email, or both address and postal code must be provided.", new string[] { nameof(Email), nameof(Address), nameof(PostalCode) })); } //Validate Postal Code var country = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode); string countryCode = country.CountryCode; bool isValid = true; string postalCode = PostalCode; //Validate Canadian Postal Code if (countryCode == "CA") { postalCode = postalCode.Trim(); isValid = SKValidations.SKPostalCodeValidation(ref postalCode); if (isValid == false) { yield return(new ValidationResult( "Postal (Zip) Code is not a valid Canadian pattern: A6A 6A6 or A6A6A6", new string[] { nameof(PostalCode) })); } else { PostalCode = postalCode; } } // Validate US Zip Code else if (countryCode == "US") { isValid = SKValidations.SKZipCodeValidation(ref postalCode); if (isValid == false) { yield return(new ValidationResult( "Postal (Zip) Code is not a valid US pattern: 12345 or 12345-1234", new string[] { nameof(PostalCode) })); } else { PostalCode = postalCode; } } //If both home and cell phone not provided if (string.IsNullOrEmpty(HomePhone) == true && string.IsNullOrEmpty(CellPhone) == true) { yield return(new ValidationResult( "Either one of Home Phone or Cell Phone is required", new string[] { nameof(HomePhone), nameof(CellPhone) })); } //If home phone provided if (string.IsNullOrWhiteSpace(HomePhone) == false) { //Extract phone number string numString = ""; foreach (char c in HomePhone) { if (char.IsDigit(c)) { numString += c; } } //Check if phone number is 10 digits long if (numString.Length != 10) { yield return(new ValidationResult( $"Not a valid phone number, must be 10 digits.", new string[] { nameof(HomePhone) })); } //Format Cell Phone number before writing to Database else { HomePhone = String.Format("{0:###-###-####}", double.Parse(numString)); } } if (string.IsNullOrWhiteSpace(CellPhone) == false) { //Check if phone number is 10 digits long string numString = ""; foreach (char c in CellPhone) { if (char.IsDigit(c)) { numString += c; } } if (numString.Length != 10) { yield return(new ValidationResult( $"Not a valid phone number, must be 10 digits.", new string[] { nameof(CellPhone) })); } //Format Cell Phone number before writing to Database else { CellPhone = String.Format("{0:###-###-####}", double.Parse(numString)); } } //Last Contact Date cannot be provided unless DateJoined is available (but the reverse is allowed) if (LastContactDate != null && DateJoined == null) { yield return(new ValidationResult( "Last Contact date cannot be provided unless Date Joined is available", new string[] { nameof(LastContactDate) })); } //A farmer cannot be contacted before they have joined the program. if (DateJoined != null && LastContactDate != null && (DateJoined > LastContactDate)) { yield return(new ValidationResult( "A farmer cannot be contacted before they have joined the program.", new string[] { nameof(LastContactDate) })); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { PatientsContext _context = new PatientsContext(); if (string.IsNullOrEmpty(FirstName) || FirstName == " ") { yield return(new ValidationResult("First name is a required field and cannot be blank spaces", new[] { "FirstName" })); } else { FirstName = FirstName.Trim(); FirstName = MBValidations.MBCapitalize(FirstName); } if (string.IsNullOrEmpty(LastName) || LastName == " ") { yield return(new ValidationResult("Last name is a required field and should not be blank space", new[] { "LastName" })); } else { LastName = LastName.Trim(); LastName = MBValidations.MBCapitalize(LastName); } if (string.IsNullOrEmpty(Gender) || Gender == " ") { yield return(new ValidationResult("Gender is a required field and should not start with a blank space", new[] { "Gender" })); } else { Gender = Gender.Trim(); Gender = MBValidations.MBCapitalize(Gender); } if (!string.IsNullOrEmpty(Address)) { Address = Address.Trim(); Address = MBValidations.MBCapitalize(Address); } if (!string.IsNullOrEmpty(City)) { City = City.Trim(); City = MBValidations.MBCapitalize(City); } if (!string.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.Trim(); Province Pro = new Province(); string error = ""; ProvinceCode = ProvinceCode.ToUpper(); try { Pro = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault(); //var country = Pro.CountryCode; } catch (Exception e) { error = e.GetBaseException().Message; } if (Pro == null) { yield return(new ValidationResult(error, new[] { nameof(ProvinceCode) })); } else { if (PostalCode != null) { PostalCode = PostalCode.Trim(); bool val = false; string USZipCode = PostalCode; if (Pro.CountryCode == "CA") { var x = Pro.FirstPostalLetter; char[] charArr = x.ToCharArray(); foreach (char ch in charArr) { if (Convert.ToChar(PostalCode.Substring(0, 1).ToUpper()) == ch) { //if(PostalCode.StartsWith(ch)) val = true; } } if (!val) { yield return(new ValidationResult("Postal code entered is not a valid code for the selected province", new[] { "PostalCode" })); } if (!MBValidations.MBPostalCodeValidation(PostalCode)) { yield return(new ValidationResult("Postal code entered is not in valid format", new[] { "PostalCode" })); } else { PostalCode = MBValidations.MBPostalCodeFormat(PostalCode); } } if (Pro.CountryCode == "US") { if (!MBValidations.MBZipCodeValidation(ref USZipCode)) { yield return(new ValidationResult("Zip code entered is not in valid format", new[] { "PostalCode" })); } else { PostalCode = USZipCode; } } } } } if (Ohip != null) { Ohip = Ohip.ToUpper(); Regex pattern = new Regex(@"^\d\d\d\d-\d\d\d-\d\d\d-[a-z][a-z]$", RegexOptions.IgnoreCase); if (!pattern.IsMatch(Ohip)) { yield return(new ValidationResult("The value for OHIP entered is not in valid format", new[] { "Ohip" })); } } if (HomePhone != null) { HomePhone = HomePhone.Trim(); if (HomePhone.Length != 10) { yield return(new ValidationResult("The Home Phone Number entered should be exactly 10 digits", new[] { "HomePhone" })); } HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); } if (DateOfBirth != null) { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("The date of Birth cannot be greater than current date", new[] { "DateOfBirth" })); } } if (Deceased) { if (DateOfDeath == null) { yield return(new ValidationResult("The date of death is required if the deceased checkbox is chec", new[] { "DateOfDeath" })); } } else { DateOfDeath = null; } if (DateOfDeath != null) { if (DateOfDeath > DateTime.Now || DateOfDeath < DateOfBirth) { yield return(new ValidationResult("The date of death cannot be greater than current date and before the Date of birth", new[] { "DateOfBirth" })); } } if (Gender != "M" && Gender != "F" && Gender != "X") { yield return(new ValidationResult("The value for gender entered must be M, F or X", new[] { "Gender" })); } yield return(ValidationResult.Success); }
//1dii creates a validate method public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { OECContext _context = OEC_Singleton.Context(); //trimm all strings of leading and trailing spaces if (Name != null) { Name = Name.ToLower().Trim(); Name = ADValidation.ADCapitalize(Name); } if (Address != null) { Address = Address.ToLower().Trim(); Address = ADValidation.ADCapitalize(Address); } if (Town != null) { Town = Town.ToLower().Trim(); Town = ADValidation.ADCapitalize(Town); } if (County != null) { County = County.ToLower().Trim(); County = ADValidation.ADCapitalize(County); } if (ProvinceCode != null) { ProvinceCode = ProvinceCode.Trim(); ProvinceCode = ProvinceCode.ToUpper(); } if (PostalCode != null) { PostalCode = PostalCode.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Directions != null) { Directions = Directions.Trim(); } //either the town or country must be provided if (String.IsNullOrWhiteSpace(Town) && String.IsNullOrWhiteSpace(County)) { yield return(new ValidationResult("At least one town or country must be provided.")); } if (String.IsNullOrWhiteSpace(Email)) { if (String.IsNullOrWhiteSpace(Address) || String.IsNullOrWhiteSpace(PostalCode)) { yield return(new ValidationResult("If no email, you must provide an address and postal code.")); } } //if postal code provided, validate and format it using postalcodevalidation or zipcode validation, depending which country if (!String.IsNullOrWhiteSpace(PostalCode)) { if (!String.IsNullOrWhiteSpace(ProvinceCode)) { var countryCode = ""; if (ProvinceCode.Length == 2) { countryCode = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode).CountryCode; } else { countryCode = _context.Province.SingleOrDefault(p => p.Name == ProvinceCode).CountryCode; } string postalCode = PostalCode; if (countryCode == "CA") { if (!ADValidation.ADPostalCodeValidation(ref postalCode)) { yield return(new ValidationResult("Postal Code is not a valid pattern (N5G 6Z6)", new string[] { nameof(PostalCode) })); } else { PostalCode = postalCode; } } if (countryCode == "US") { if (!ADValidation.ADZipCodeValidation(ref postalCode)) { yield return(new ValidationResult("Zip Code is not a valid pattern (12345 or 12345-1234)", new string[] { nameof(PostalCode) })); } PostalCode = postalCode; } } } //either home phone or cellphone must be provided if (String.IsNullOrWhiteSpace(HomePhone) && String.IsNullOrWhiteSpace(CellPhone)) { yield return(new ValidationResult("You must provide either your cell or home phone number.")); } else { Regex pattern = new Regex(@"\d{10}", RegexOptions.IgnoreCase); if (!String.IsNullOrWhiteSpace(HomePhone)) { //get rid of all punctuation and trim and space HomePhone = Regex.Replace(HomePhone, @"[^\w\s]", "").Trim(); HomePhone = Regex.Replace(HomePhone, "[^0-9]", ""); if (!pattern.IsMatch(HomePhone)) { yield return(new ValidationResult("Home phone is incorrect pattern: ", new string[] { nameof(HomePhone) })); } else { //insert dashes into phone number HomePhone = Regex.Replace(HomePhone, @"^(...)(...)(....)$", "$1-$2-$3"); } } if (!String.IsNullOrWhiteSpace(CellPhone)) { //get rid of all punctuation and trim and space CellPhone = Regex.Replace(CellPhone, @"[^\w\s]", "").Trim(); CellPhone = Regex.Replace(CellPhone, "[^0-9]", ""); if (!pattern.IsMatch(CellPhone)) { yield return(new ValidationResult("Cell phone is incorrect pattern: ", new string[] { nameof(CellPhone) })); } else { //insert dashes into phone number CellPhone = Regex.Replace(CellPhone, @"^(...)(...)(....)$", "$1-$2-$3"); } } } //lastcontact date cantbe provided unless datejoined if (LastContactDate != null && DateJoined == null) { yield return(new ValidationResult("You must also provide Date Joined.", new string[] { nameof(DateJoined) })); } //last contact date cant be before date joined if (DateJoined > LastContactDate) { yield return(new ValidationResult("Last contact date can not be prior to date joined.")); } //1diii replace the throw statement yield return(ValidationResult.Success); //throw new NotImplementedException(); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (FirstName == null) { FirstName = string.Empty; } else { FirstName = FirstName.Trim(); FirstName = ASValidations.ASCapitalize(FirstName).ToString(); } if (LastName == null) { LastName = string.Empty; } else { LastName = LastName.Trim(); LastName = ASValidations.ASCapitalize(LastName).ToString(); } if (Gender == null) { Gender = string.Empty; } else { Gender = Gender.Trim(); Gender = ASValidations.ASCapitalize(Gender).ToString(); } if (Address == null) { Address = string.Empty; } else { Address = Address.Trim(); Address = ASValidations.ASCapitalize(Address).ToString(); } if (City == null) { City = string.Empty; } else { City = City.Trim(); City = ASValidations.ASCapitalize(City).ToString(); } if (ProvinceCode != null) { var provSearch = _context.Province.Where(p => p.ProvinceCode == ProvinceCode).FirstOrDefault(); if (provSearch == null) { yield return(new ValidationResult("Province code is not on file", new[] { nameof(ProvinceCode) })); yield return(new ValidationResult("Province code is required to validate Postal Code", new[] { nameof(PostalCode) })); } else { var countrySearch = _context.Country.Where(a => a.CountryCode == provSearch.CountryCode).FirstOrDefault(); if (provSearch.CountryCode == "CA") { ProvinceCode = ProvinceCode.Trim(); ProvinceCode = ProvinceCode.ToUpper(); if (PostalCode != null || PostalCode == "") { PostalCode = PostalCode.Trim().ToUpper(); if (provSearch.FirstPostalLetter.Contains(PostalCode.Substring(0, 1)) == false) { yield return(new ValidationResult("First letter of postal code not valid for given province", new[] { nameof(PostalCode) })); } else { if (ASValidations.ASPostalCodeValidation(PostalCode) == false) { yield return(new ValidationResult("Postal code not in cdn format: A3A 3A3", new[] { nameof(PostalCode) })); } else { PostalCode = string.Format("{0} {1}", PostalCode.Substring(0, 3), PostalCode.Substring(3)); } } } } else if (provSearch.CountryCode == "US") { ProvinceCode = ProvinceCode.Trim(); if (!String.IsNullOrEmpty(PostalCode)) { PostalCode = PostalCode.Trim(); if (!ASValidations.ASZipCodeValidation(PostalCode)) { yield return(new ValidationResult("US Zip code format incorrect: 12345 / 12345-6789", new[] { nameof(PostalCode) })); } } } } } if ((Deceased == true) && (DateOfDeath == null)) { yield return(new ValidationResult("If deceased is true, a date of death is required", new[] { nameof(DateOfDeath) })); } else if ((Deceased == false) && (DateOfDeath != null)) { yield return(new ValidationResult("Deceased must be true, if Date of Death is provided", new[] { nameof(Deceased) })); } if (Ohip != null) { Ohip = Ohip.ToUpper(); } if (HomePhone != null) { HomePhone = HomePhone.Trim(); var HomePhoneNumber = ASValidations.ASExtractDigits(HomePhone).ToString(); if (HomePhoneNumber.Length != 10) { yield return(new ValidationResult("Home Phone if provided, should be 10 digits: 123-123-1234", new[] { nameof(HomePhone) })); } else { HomePhone = string.Format("{0}-{1}-{2}", HomePhoneNumber.Substring(0, 3), HomePhoneNumber.Substring(3, 3), HomePhoneNumber.Substring(6)); } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { OECContext _context = OEC_Singleton.Context(); FarmId = Convert.ToInt32(FarmId.ToString().Trim()); if (Name != null) { Name = Name.Trim(); Name = HKValidations.HKCapitalize(Name); } if (Address != null) { Address = Address.Trim(); Address = HKValidations.HKCapitalize(Address); } if (Town != null) { Town = Town.Trim(); Town = HKValidations.HKCapitalize(Town); } if (County != null) { County = County.Trim(); County = HKValidations.HKCapitalize(County); } if (ProvinceCode != null) { ProvinceCode = ProvinceCode.Trim(); ProvinceCode = ProvinceCode.ToUpper(); } if (PostalCode != null) { PostalCode = PostalCode.Trim(); } if (HomePhone != null) { HomePhone = HomePhone.Trim(); } if (CellPhone != null) { CellPhone = CellPhone.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Directions != null) { Directions = Directions.Trim(); } if (String.IsNullOrWhiteSpace(Name) || String.IsNullOrWhiteSpace(ProvinceCode)) { if (String.IsNullOrWhiteSpace(Name)) { yield return(new ValidationResult("Name is required", new[] { "Name" })); } if (String.IsNullOrWhiteSpace(ProvinceCode)) { yield return(new ValidationResult("Province Code is required", new[] { "ProvinceCode" })); } } if (String.IsNullOrWhiteSpace(Town) && String.IsNullOrWhiteSpace(County)) { yield return(new ValidationResult("Either the town or county must be provided.", new[] { "Town", "County" })); } if (String.IsNullOrWhiteSpace(Email)) { if (string.IsNullOrWhiteSpace(Address)) { yield return(new ValidationResult("Address must be provided.", new[] { "Address" })); } if (string.IsNullOrWhiteSpace(PostalCode)) { yield return(new ValidationResult("Postal Code must be provided.", new[] { "PostalCode" })); } } if (!String.IsNullOrEmpty(PostalCode) && !String.IsNullOrWhiteSpace(ProvinceCode)) { string countryCode = ""; if (ProvinceCode.Length == 2) { countryCode = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode).CountryCode; } else { countryCode = _context.Province.SingleOrDefault(p => p.Name == ProvinceCode).CountryCode; } if (countryCode == "CA") { string pCode = PostalCode; if (!HKValidations.HKPostalCodeValidation(ref pCode)) { yield return(new ValidationResult("Please enter valid postal code.", new[] { "PostalCode" })); } PostalCode = pCode; } else if (countryCode == "US") { string zCode = PostalCode; if (!HKValidations.HKZipCodeValidation(ref zCode)) { yield return(new ValidationResult("Please enter valid zip code.", new[] { "PostalCode" })); } PostalCode = zCode; } } if (String.IsNullOrWhiteSpace(HomePhone) && String.IsNullOrWhiteSpace(CellPhone)) { yield return(new ValidationResult("Either the home phone number or cell phone number must be provided.", new[] { "HomePhone", "CellPhone" })); } else { if (!String.IsNullOrWhiteSpace(HomePhone)) { string hPhone = HomePhone; if (!HKValidations.HKPhoneNumberValidation(ref hPhone)) { yield return(new ValidationResult("Home phone number must be 10 digits only.", new[] { "HomePhone" })); } HomePhone = hPhone; } if (!String.IsNullOrWhiteSpace(CellPhone)) { string cPhone = CellPhone; if (!HKValidations.HKPhoneNumberValidation(ref cPhone)) { yield return(new ValidationResult("Cell phone number must be 10 digits only.", new[] { "CellPhone" })); } HomePhone = cPhone; } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { // first name FirstName = FirstName.Trim(); FirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(FirstName); // last name LastName = LastName.Trim(); LastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(LastName); // spouse first name SpouseFirstName = SpouseFirstName.Trim(); SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName); // spouse last name SpouseLastName = SpouseLastName.Trim(); SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName); if (String.IsNullOrEmpty(SpouseFirstName)) { SpouseFirstName = null; } else { SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName.Trim()); } if (String.IsNullOrEmpty(SpouseLastName)) { SpouseLastName = null; } else { SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName.Trim()); } // street Street = Street.Trim(); Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street); if (String.IsNullOrEmpty(Street)) { Street = null; } else { Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street.Trim()); } // city City = City.Trim(); City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City); if (String.IsNullOrEmpty(City)) { City = null; } else { City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City.Trim()); } // postal code if (PostalCode == null || PostalCode == "") { PostalCode = ""; } else { PostalCode = PostalCode.Trim(); if (HMBPClassLibrary.HMBPValidations.HMBPPostalCodeValidation(PostalCode)) { PostalCode = HMBPClassLibrary.HMBPValidations.HMBPPostalCodeFormat(PostalCode); } else { string _postalCode = ""; _postalCode = PostalCode; if (HMBPClassLibrary.HMBPValidations.HMBPZipCodeValidation(ref _postalCode)) { PostalCode = _postalCode; } else { yield return(new ValidationResult("error", new[] { "PostalCode" })); } } if (ProvinceCode == null) { yield return(new ValidationResult("error", new[] { "ProvinceCode" })); } } // email if (string.IsNullOrEmpty(Email)) { Email = null; } else // trim { Email = Email.Trim(); } // comment if (string.IsNullOrEmpty(Comment)) { Comment = null; } else // trim { Comment = Comment.Trim(); } //home phone reformat HomePhone = HMBPClassLibrary.HMBPValidations.HMBPExtractDigits(HomePhone.Trim()); if (HomePhone.Length != 10) { yield return(new ValidationResult("The home phone can only contain 10 digits", new[] { nameof(HomePhone) })); } else { HomePhone = HomePhone.Insert(3, "-").Insert(7, "-"); } //validate Joined year if (YearJoined.HasValue) { if (YearJoined > DateTime.Now.Year) { YearJoined = null; yield return(new ValidationResult("The year cant be in the future", new[] { nameof(YearJoined) })); } } //Full name if (String.IsNullOrEmpty(SpouseFirstName) && String.IsNullOrEmpty(SpouseLastName)) { FullName = LastName + ", " + FirstName; } else if (!String.IsNullOrEmpty(SpouseFirstName)) { if (String.IsNullOrEmpty(SpouseLastName) || SpouseLastName == LastName) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else if (!String.IsNullOrEmpty(SpouseLastName)) { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } } yield return(ValidationResult.Success); }
public override string ToString() { return(string.Format("{0}, {1}, {2}, {3}, {4}", Name.Trim(), Address1.Trim(), City.Trim(), State.Trim(), PostalCode.Trim())); }
//self_validation for memebr class public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { SailContext _context = new SailContext(); //trim all strings FirstName = FirstName.Trim(); LastName = LastName.Trim(); if (SpouseFirstName != null) { SpouseFirstName = SpouseFirstName.Trim(); } if (SpouseLastName != null) { SpouseLastName = SpouseLastName.Trim(); } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (ProvinceCode != null) { ProvinceCode = ProvinceCode.Trim(); } if (PostalCode != null) { PostalCode = PostalCode.Trim(); } HomePhone = HomePhone.Trim(); if (Email != null) { Email = Email.Trim(); } //capitalize designated fields FirstName = HXClassLibrary.HXValidations.HXCapitalize(FirstName); LastName = HXClassLibrary.HXValidations.HXCapitalize(LastName); SpouseFirstName = HXClassLibrary.HXValidations.HXCapitalize(SpouseFirstName); SpouseLastName = HXClassLibrary.HXValidations.HXCapitalize(SpouseLastName); Street = HXClassLibrary.HXValidations.HXCapitalize(Street); City = HXClassLibrary.HXValidations.HXCapitalize(City); // format full name if (SpouseFirstName == "") { FullName = LastName + ", " + FirstName; } else if (SpouseFirstName != "" && SpouseLastName != "") { if (SpouseLastName == LastName) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } } else if (SpouseFirstName != "" && SpouseLastName == "") { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } //validate province code Province province = null; string errorMessage = ""; try { ProvinceCode = (ProvinceCode + "").ToUpper(); province = _context.Province.Find(ProvinceCode); if (province == null) { errorMessage = "the province code is not on file"; } } catch (Exception ex) { errorMessage = $"fetching provinceCode error: {ex.GetBaseException().Message}"; } if (errorMessage != "") { yield return(new ValidationResult( errorMessage, new[] { "ProvinceCode" })); } //validate postal code if (!string.IsNullOrEmpty(PostalCode)) { if (string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult( "province code is required when having the postal code", new[] { "ProvinceCode" })); } else { if (province == null) { yield return(new ValidationResult( "The province code is not on file", new[] { "ProvinceCode" })); } else { if (province.CountryCode == "CA") { if (HXClassLibrary.HXValidations.HXPostalCodeValidation(PostalCode)) { HXClassLibrary.HXValidations.HXPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult( "the postal code is invalid in Canada (it should follow the format: A1B 1E1)", new[] { "PostalCode" })); } } else if (province.CountryCode == "US") { string postalCode = PostalCode; if (HXClassLibrary.HXValidations.HXZipCodeValidation(ref postalCode)) { PostalCode = postalCode; } else { yield return(new ValidationResult( "the zip code is invalid in the US (it should have 5 or 9 digits)", new[] { "PostalCode" })); } } } } } //validate home phone if (HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Length == 10) { HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Insert(3, "-").Insert(6, "-"); } else { HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone); yield return(new ValidationResult( "a valid phone number should have 10 digits", new[] { "HomePhone" })); } //validate year joined if (YearJoined > Convert.ToInt32(DateTime.Now.Year)) { yield return(new ValidationResult( "year joined can not be in the future", new[] { "YearJoined" })); } var memberId = _context.Member.Find(MemberId); if (memberId == null) { if (YearJoined == null) { yield return(new ValidationResult( "year joined can only be null for existing records.", new[] { "YearJoined" })); } } //validate using canada post if (UseCanadaPost == false) { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult( "Must have a valid email if not using Canada Post", new[] { "Email" })); } } else { if (string.IsNullOrEmpty(Street) || string.IsNullOrEmpty(City) || string.IsNullOrEmpty(ProvinceCode) || string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult( "street, city, province code, postal code can not be null when using Canada Post.", new[] { "UseCanadaPost" })); } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("Province code cannot be longer than 2 characters", new[] { "ProvinceCode" })); } else { var provinceContext = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); if (provinceContext == null) { yield return(new ValidationResult("Invalid province code", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex postalCodeRegex = new Regex((@"^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}\s{0,1}[0-9]{1}[A-Za-z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCode != null) { if (postalCodeRegex.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("Invalid postal code. Postal code must match Canadian postal pattern", new[] { "PostalCode" })); } } Regex homePhoneRegex = new Regex(@"^[0-9]{3}-{0,1}[0-9]{3}-{0,1}[0-9]{4}"); if (HomePhone != null) { if (homePhoneRegex.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("Invalid Phone Number Format. It must be in the format : 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + ", " + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseLastName.Trim() + ", " + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street)) { yield return(new ValidationResult("If Canada post is checked, street name is required", new[] { "Street" })); } if (string.IsNullOrEmpty(City)) { yield return(new ValidationResult("If Canada post is checked, city name is required", new[] { "City" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("If Canada post is not checked, email address is required", new[] { "Email" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } if (LastName != null) { LastName = LastName.Trim(); } if (FirstName != null) { FirstName = FirstName.Trim(); } if (SpouseFirstName != null) { SpouseFirstName = SpouseFirstName.Trim(); } if (SpouseLastName != null) { SpouseLastName = SpouseLastName.Trim(); } //determine if editing or creating new var memberId = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (memberId != null) { //yield error : member id is already on file } else { //yield error: member id not on file } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //if (MemberId != 0) //{ // FullName = FirstName + ' ' + LastName + " & " + SpouseFirstName; //} if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("The Province Code should be exactly 2 characters long", new[] { "ProvinceCode" })); } else { var province = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault(); if (province == null) { yield return(new ValidationResult("The Province Code is not valid", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex PostalCodePattern = new Regex((@"^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s{0,1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCodePattern.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("The Postal Code entered is not in valid canadian format", new[] { "PostalCode" })); } Regex HomePhonePattern = new Regex(@"^\d\d\d-{0,1}\d\d\d-{0,1}\d\d\d\d"); if (HomePhone != null) { if (HomePhonePattern.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("The home Phone number entered is not in valid format 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + "," + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = FirstName.Trim() + ' ' + LastName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + "," + FirstName.Trim() + " & " + SpouseLastName.Trim() + "," + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street) && string.IsNullOrEmpty(City)) { yield return(new ValidationResult("The Street name and City Name field are required fields if you have checked Canada Post checkbox", new[] { "Street" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("The Email address field is required", new[] { "Email" })); } } if (MemberId == 0) { var duplicateID = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (duplicateID != null) { yield return(new ValidationResult("The Member Id entered is already on file", new[] { "MemberId" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { OECContext _context = JHOECContext_Singleton.Context(); //trim empty spaces for all fields if (!string.IsNullOrEmpty(PostalCode)) { PostalCode = PostalCode.Trim(); } if (!string.IsNullOrEmpty(HomePhone)) { HomePhone = HomePhone.Trim(); } if (!string.IsNullOrEmpty(CellPhone)) { CellPhone = CellPhone.Trim(); } if (!string.IsNullOrEmpty(Email)) { Email = Email.Trim(); } if (!string.IsNullOrEmpty(Directions)) { Directions = Directions.Trim(); } //capitalize name if (!string.IsNullOrEmpty(Name.Trim())) { Name.Trim(); Name = JHValidations.JHCapitalize(Name); } else { yield return(new ValidationResult("Name cannot be empty strings", new string[] { nameof(Name) })); } //capitalize address if (!string.IsNullOrEmpty(Address)) { Address = Address.Trim(); Address = JHValidations.JHCapitalize(Address); } //else //{ // yield return new ValidationResult("Address cannot be empty strings", new string[] { nameof(Address)}); //} //capitalize town if (!string.IsNullOrEmpty(Town)) { Town = Town.Trim(); Town = JHValidations.JHCapitalize(Town); } //else //{ // yield return new ValidationResult("Town cannot be empty strings", new string[] { nameof(Town) }); //} //capitalize county if (!string.IsNullOrEmpty(County)) { County = County.Trim(); County = JHValidations.JHCapitalize(County); } //else //{ // yield return new ValidationResult("County cannot be empty strings", new string[] { nameof(County) }); //} //either town or county must be provided if (string.IsNullOrEmpty(County) && string.IsNullOrEmpty(Town)) { yield return(new ValidationResult("Either County or Town must be provided", new[] { nameof(Town), nameof(County) })); } //if email is not provided, address and postal must be provided if (string.IsNullOrEmpty(Email) && string.IsNullOrEmpty(Address) || string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult("Either Email or Address and Postal Code must be provided", new[] { nameof(Address), nameof(PostalCode), nameof(Email) })); } //force province code to upper case if (!string.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.Trim(); ProvinceCode = ProvinceCode.ToUpper(); } //validate postcode based on country if (_context.Province.Where(p => p.ProvinceCode == ProvinceCode).Select(p => p.CountryCode).FirstOrDefault() == "CA") { var postalCodeCheck = PostalCode; var validateResult = false; validateResult = JHValidations.JHPostalCodeValidation(ref postalCodeCheck); if (validateResult == true) { PostalCode = postalCodeCheck; } else { yield return(new ValidationResult(" Postal Code is invalid. The format doesnt match 'A3B C4B'", new[] { nameof(PostalCode) })); } } else if (_context.Province.Where(p => p.ProvinceCode == ProvinceCode).Select(p => p.CountryCode).FirstOrDefault() == "US") { var postalCodeCheck = PostalCode; var validateResult = false; validateResult = JHValidations.JHZipCodeValidation(ref postalCodeCheck); if (validateResult == true) { PostalCode = postalCodeCheck; } else { yield return(new ValidationResult(" Zip Code is invalid. The format doesnt match '12345' or '12345-6789'", new[] { nameof(PostalCode) })); } } //last contacted date cannot before join date, AND cannot exist if the farm hasn't joined yet if (DateJoined == null && LastContactDate != null) { yield return(new ValidationResult("Last contacted date doesnt exist if the farm hasn't joined yet", new[] { nameof(LastContactDate), nameof(DateJoined) })); } if (DateJoined != null && LastContactDate != null) { if (DateJoined > LastContactDate) { yield return(new ValidationResult("Last contaced date cound not exist before the join date", new[] { nameof(LastContactDate), nameof(DateJoined) })); } } //validate phone number if (string.IsNullOrEmpty(CellPhone) && string.IsNullOrEmpty(HomePhone)) { yield return(new ValidationResult("Either cell phone or home phone must be provided", new[] { nameof(CellPhone), nameof(HomePhone) })); } if (!string.IsNullOrEmpty(CellPhone)) { var phoneCheck = CellPhone; var validateResult = false; validateResult = JHValidations.JHPhoneValidate(ref phoneCheck); if (validateResult == true) { CellPhone = phoneCheck; } else { yield return(new ValidationResult(" Cell phone number is invalid. The format doesnt match '123-456-7890'", new[] { nameof(CellPhone) })); } } if (!string.IsNullOrEmpty(HomePhone)) { var phoneCheck = HomePhone; var validateResult = false; validateResult = JHValidations.JHPhoneValidate(ref phoneCheck); if (validateResult == true) { HomePhone = phoneCheck; } else { yield return(new ValidationResult(" Home phone number is invalid. The format doesnt match '123-456-7890'", new[] { nameof(HomePhone) })); } } yield return(ValidationResult.Success); }