public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { FirstName = BPValidations.Capitalise(FirstName); LastName = BPValidations.Capitalise(LastName); FullName = LastName + "," + FirstName; if (ProvinceCode != null) { ProvinceCode = ProvinceCode.ToUpper(); } if (PostalCode != null) { PostalCode = PostalCode.ToUpper(); if (PostalCode[3] != ' ') { PostalCode = PostalCode.Insert(3, " "); } } //HomePhone and WorkPhone Validation HomePhone = BPValidations.FormatPhoneNumber(HomePhone); if (WorkPhone != null) { WorkPhone = BPValidations.FormatPhoneNumber(WorkPhone); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (String.IsNullOrEmpty(ProvinceName)) { yield return(new ValidationResult("Province Name cannot be null", new[] { nameof(ProvinceName) })); } else { ProvinceName = Validations.Capitalize(ProvinceName); } if (String.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("Province Code cannot be null.", new[] { nameof(ProvinceCode) })); } ProvinceCode = ProvinceCode.Trim().ToUpper(); if (ProvinceCode.Length > 2) { yield return(new ValidationResult("Province Code cannot be greater than 2 characters", new[] { nameof(ProvinceCode) })); } yield return(ValidationResult.Success); }
public override int GetHashCode() { unchecked { var hashCode = (CityCode != null ? CityCode.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (City != null ? City.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Province != null ? Province.GetHashCode() : 0); hashCode = (hashCode * 397) ^ ProvinceCode.GetHashCode(); return(hashCode); } }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { // Format names FirstName = ClassLibrary.Validations.Capitalize(FirstName); LastName = ClassLibrary.Validations.Capitalize(LastName); FullName = string.Format("{0}, {1}", LastName, FirstName); // check privince code; in database; upper case string message = ""; try { ProvinceCode = ProvinceCode.ToUpper(); if (!ProvinceCodeExists(ProvinceCode)) { message = string.Format("{0} is not a valid province code, please ensure it is only two letters", ProvinceCode); } } catch (Exception e) { message = string.Format("Unknown error occured: {0}. Please enter a 2 digit code", e.ToString()); } if (message != "") { yield return(new ValidationResult(message)); } // add in final format Q1A 1A1, custom validator validates the format formatPostalCode(); // check home phone is required, work optional; check formatting as well // writing phone to database requires the format 519-555-1234 bool homePhoneValid = validatePhoneNumber(HomePhone, false); if (homePhoneValid) { HomePhone = formatPhoneNumber(HomePhone); } else { yield return(new ValidationResult(string.Format("Phone number {0} was not valid, please enter a 10 digit phone number (i.e. 519-123-4567)", HomePhone))); } bool workPhoneValid = validatePhoneNumber(WorkPhone, true); if (workPhoneValid) { WorkPhone = formatPhoneNumber(WorkPhone); } else { yield return(new ValidationResult(string.Format("Phone number {0} was not valid, please leave empty or enter a 10 digit number (i.e. 519-123-4567", WorkPhone))); } // date is required and cannot be in future; display like 23 Oct 2013 in form yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { FirstName = MKValidation.Capitalise(FirstName); LastName = MKValidation.Capitalise(LastName); FullName = LastName + ", " + FirstName; HomePhone = Phoneverification(HomePhone); WorkPhone = Phoneverification(WorkPhone); ProvinceCode = ProvinceCode.ToUpper(); PostalCode = PostalCode.ToUpper(); if (PostalCode.Length == 6) { PostalCode = PostalCode.Insert(3, " "); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { PatientsContext _context = new PatientsContext(); //c.Non-blank firstName, lastName and gender are required FirstName = YKCapitalize(FirstName); if (FirstName == "") { yield return(new ValidationResult("First name cannot be empty or just blanks", new[] { nameof(FirstName) })); } LastName = YKValidations.YKCapitalize(LastName); if (LastName == "") { yield return(new ValidationResult("Last name cannot be empty or just blanks", new[] { nameof(LastName) })); } //Capitalise address, city and gender if (Address != null) { Address = YKValidations.YKCapitalize(Address); } if (City != null) { City = YKValidations.YKCapitalize(City); } //If provinceCode is not empty or null, if (ProvinceCode != null) { // force it to upper case ProvinceCode = ProvinceCode.Trim().ToUpper(); // i.Validate it by fetching its record from the database … error if not found var provinceCode = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).Any(); if (!provinceCode) { yield return(new ValidationResult("Province Code is not on file", new[] { nameof(ProvinceCode) })); } else { if (PostalCode == null) { yield return(new ValidationResult("Postal Code is needed when Province Code is given", new[] { nameof(PostalCode) })); } } } //f.postalCode is conditionally optional but, if provided: if (PostalCode != null) { //i.Produce an error if provinceCode is invalid / missing … it’s required to edit a postal/ zip code if (ProvinceCode == null) { yield return(new ValidationResult("Province Code is needed when Postal Code is given.", new[] { nameof(ProvinceCode) })); } else { var countryCode = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).SingleOrDefault(); if (countryCode != null) { //ii.If provinceCode indicates the patient is in Canada, // verify that the first letter of the postalCode is correct for that province. if (countryCode.CountryCode == "CA") { if (YKValidations.YKPostalCodeValidation(PostalCode)) { PostalCode = YKValidations.YKPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult("The format of the postalCode is not correct for that province.", new[] { nameof(PostalCode) })); } } else if (countryCode.CountryCode == "US") { //iii.Validate and format the postal / zip code using the relevant method(s) in your XXValidations Class // (error if provinceCode is invalid / missing). string postalCode = PostalCode; if (YKValidations.YKZipCodeValidation(ref postalCode)) { PostalCode = postalCode; } else { yield return(new ValidationResult("The format of the postalCode is not correct for that province.", new[] { nameof(PostalCode) })); } } else { //If not, produce a pertinent error message for both fields. yield return(new ValidationResult("The first letter of the postalCode is not correct for that province.", new[] { nameof(PostalCode) })); } } } } //g.OHIP is optional, but if provided, if (Ohip != null) { if (YKValidations.YKOhipValidation(Ohip)) { //shift it to upper case and ensure it’s in this pattern: 1234 - 123 - 123 - XX. Ohip = YKValidations.YKOhipFormat(Ohip); } else { yield return(new ValidationResult("OHIP, if provided, must match pattern: 1234-123-123-XX", new[] { nameof(Ohip) })); } } //h.homePhone is optional, but if provided: //i.It must contain exactly 10 digits(discard punctuation and text like “emergency only”). if (HomePhone != null) { HomePhone = YKValidations.YKExtractDigits(HomePhone.Trim()); if (HomePhone.Length == 10) { string homePhone = HomePhone; //ii.Reformat into dash notation: 519 - 748 - 5220 HomePhone = YKValidations.YKHomePhoneFormat(homePhone); } else { yield return(new ValidationResult("The phone number's length is not enough or right format", new[] { nameof(HomePhone) })); } //iii.Sorry about historical data … we didn’t have edits.They’ll be fixed on their next visit. } //i.dateOfBirth is optional but, if provided, cannot be in the future if (DateOfBirth != null) { if (DateOfBirth >= DateTime.Now) { yield return(new ValidationResult("Date of Birth cannot be in the future", new[] { nameof(DateOfBirth) })); } } //i.If deceased is true, dateOfDeath is required. //iii.If dateOfDeath is provided, it cannot be in the future or before dateOfBirth(if it is provided) if (Deceased) { if (DateOfDeath == null) { yield return(new ValidationResult("Date of Death is needed when Deceased is checked", new[] { nameof(DateOfDeath) })); } else if (DateOfBirth == null) { yield return(new ValidationResult("Date of Birth is needed when Deceased is checked", new[] { nameof(DateOfBirth) })); } else if (DateOfDeath >= DateTime.Now) { yield return(new ValidationResult("Date of Death cannot be in the future", new[] { nameof(DateOfDeath) })); } else if (DateOfDeath < DateOfBirth) { yield return(new ValidationResult("Date of Death cannot be before Date of Birth", new[] { nameof(DateOfDeath) })); } } else { //ii.If deceased is false, dateOfDeath must be null. if (DateOfDeath != null) { yield return(new ValidationResult("Date of Death is not needed when Deceased is unchecked", new[] { nameof(DateOfDeath) })); } } //gender is required and must be “M”, “F” or “X”. //Add a field-validation <span> to display error messages on Create & Edit views. Gender = YKValidations.YKCapitalize(Gender); if (Gender == null) { yield return(new ValidationResult("Gender cannot be empty or just blanks", new[] { nameof(Gender) })); } else { if (Gender != "M" && Gender != "F" && Gender != "X") { yield return(new ValidationResult("Gender should be M, F or X", new[] { nameof(Gender) })); } } 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) { bool isValid; // Validate First Name // Check for non-blank if (FirstName == null || FirstName.Trim() == "") { yield return(new ValidationResult("First Name cannot be empty or just blanks", new[] { nameof(FirstName) })); } else { // Capitalize FirstName = NDValidations.NDCapitalize(FirstName); } // Validate Last Name // Check for non-blank if (LastName == null || LastName.Trim() == "") { yield return(new ValidationResult("Last Name cannot be empty or just blanks", new[] { nameof(LastName) })); } else { // Capitalize LastName = NDValidations.NDCapitalize(LastName); } // Validate Gender // Check for non-blank if (Gender == null || Gender.Trim() == "") { yield return(new ValidationResult("Gender cannot be empty or just blanks", new[] { nameof(Gender) })); } else { // Capitalize Gender = NDValidations.NDCapitalize(Gender); if (Gender != "M" && Gender != "F" && Gender != "X") { yield return(new ValidationResult("Gender must be either 'M', 'F' or 'X'", new[] { nameof(Gender) })); } } // Capitalize Address = NDValidations.NDCapitalize(Address); City = NDValidations.NDCapitalize(City); // Validate ProvinceCode if (ProvinceCode != null) { ProvinceCode = ProvinceCode.ToUpper(); // Check if ProvinceCode exists in DB var province = context.Province.Where(p => p.ProvinceCode == ProvinceCode).FirstOrDefault(); if (province == null) { yield return(new ValidationResult("Province Code is not on file", new[] { nameof(ProvinceCode) })); } } // Validate Postal Code if (PostalCode != null) { if (string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("Province Code is required to validate Postal Code", new[] { nameof(ProvinceCode) })); } else { // Format Postal Code to 'A3A 3A3' PostalCode = NDValidations.NDPostalCodeFormat(PostalCode); if (PostalCode == string.Empty) { yield return(new ValidationResult("Postal Code must match pattern: A3A 3A3", new[] { nameof(PostalCode) })); } isValid = NDValidations.NDIsValidPostalCodeCanada(ProvinceCode.ToUpper().Trim(), PostalCode); if (isValid == false) { yield return(new ValidationResult("First letter of Postal Code not valid for given Province", new[] { nameof(PostalCode) })); // Validate and format US Zip Code string zipCode = PostalCode; isValid = NDValidations.NDZipCodeValidation(ref zipCode); if (isValid == false) { yield return(new ValidationResult("Postal Code is invalid", new[] { nameof(PostalCode) })); } else { PostalCode = zipCode; } } } } // Validate OHIP if (Ohip != null) { isValid = NDValidations.NDOhipValidation(Ohip); if (isValid) { Ohip = Ohip.ToUpper().Trim(); } else { yield return(new ValidationResult("OHIP, if provided, must match pattern: 1234-123-123-XX", new[] { nameof(Ohip) })); } } // Validate Home Phone string phone = HomePhone; isValid = NDValidations.NDPhoneValidation(ref phone); if (!isValid) { yield return(new ValidationResult("Home Phone, if provided, must be 10 digits: 123-123-1234", new[] { nameof(HomePhone) })); } else { HomePhone = phone; } // Validate Date Of Birth not in the future if (DateOfBirth != null && DateOfBirth > DateTime.Now) { yield return(new ValidationResult("Date Of Birth cannot be in the future", new[] { nameof(DateOfBirth) })); } // Validate Date Of Deceased if (Deceased == false) { if (DateOfDeath != null) { yield return(new ValidationResult("Deceased must be true if Date of Death is provided", new[] { nameof(Deceased) })); } } else { if (DateOfDeath == null) { yield return(new ValidationResult("If Deceased is true, a Date Of Death is required", new[] { nameof(DateOfDeath) })); } else if (DateOfDeath > DateTime.Now) { yield return(new ValidationResult("Date Of Death cannot be in the future", new[] { nameof(DateOfDeath) })); } else if (DateOfDeath < DateOfBirth) { yield return(new ValidationResult("Date Of Death cannot be before Date Of Birth", new[] { nameof(DateOfDeath) })); } } }
//Method that validate and reformat inputs from user private void DAEdit() { string errorMessage = ""; Regex regex; //Trim all string fields if (ProvinceCode.Trim() == null) { ProvinceCode = ""; } if (Name.Trim() == null) { Name = ""; } if (CountryCode.Trim() == null) { CountryCode = ""; } if (TaxCode.Trim() == null) { TaxCode = ""; } //Checks Name if (Name == "") { errorMessage += "Name is required\n"; } //Checks Province Code regex = new Regex("^[a-zA-Z]{2}"); if (!regex.IsMatch(ProvinceCode)) { errorMessage += "Province Code is required and must be" + " 2 letters\n"; } else if (regex.IsMatch(ProvinceCode)) { ProvinceCode = ProvinceCode.ToUpper(); } //Checks Country Code if (!regex.IsMatch(CountryCode)) { errorMessage += "Country Code is required and must be" + " 2 letters\n"; } else if (regex.IsMatch(CountryCode)) { CountryCode = CountryCode.ToUpper(); } if (TaxCode != "") { TaxCode = TaxCode.ToUpper(); } //Checks Tax Code regex = new Regex("^[a-zA-Z]*"); if (!regex.IsMatch(TaxCode)) { errorMessage += "Tax Code must be only letters\n"; } //Checks Tax Rate if (TaxCode == "" && TaxRate != 0) { errorMessage += "Insert first a Tax Code, then a Tax Rate\n"; } else if (TaxCode != "" && (TaxRate < 0 || TaxRate > 1)) { errorMessage += "Tax Rate must be between 0 and 1, inclusive\n"; } //Checks federal tax if (TaxRate == 0 && IncludesFederalTax) { errorMessage += "Includes Federal Tax must not be selected" + " if Tax Rate is zero\n"; } //Checks if Province code already exists if (DAGetByProvinceCode(ProvinceCode) != null) { IsEdit = true; } else { IsEdit = false; } //Checks to update if ( DAGetByProvinceName(Name) != null && (DAGetByProvinceCode(ProvinceCode) == null || DAGetByProvinceCode(ProvinceCode).ProvinceCode != DAGetByProvinceName(Name).ProvinceCode)) { errorMessage += "The name entered already exists!\n"; } if (errorMessage != "") { throw new Exception(errorMessage); } }
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); }
//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) { var patientProvince = new Province(); if (FirstName == null || FirstName.Trim() == "") { yield return(new ValidationResult("First Name cannot be empty or just blanks", new[] { nameof(FirstName) })); } else { FirstName = SBValidations.SBCapitaize(FirstName); } if (LastName == null || LastName.Trim() == "") { yield return(new ValidationResult("Last Name cannot be empty or just blanks", new[] { nameof(LastName) })); } else { LastName = SBValidations.SBCapitaize(LastName); } if (ProvinceCode != null) { if (ProvinceCode.Trim() != "") { ProvinceCode = ProvinceCode.Trim().ToUpper(); _context = (PatientsContext)validationContext.GetService(typeof(PatientsContext)); if (!_context.Province.Any(a => a.ProvinceCode == ProvinceCode)) { yield return(new ValidationResult("Please enter valid Province Code", new[] { nameof(ProvinceCode) })); } else { patientProvince = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); } } } if (PostalCode != null) { if (patientProvince.CountryCode == "CA") { string firstCharInPostalCode = PostalCode.Substring(0, 1).ToUpper(); if (!patientProvince.FirstPostalLetter.Contains(firstCharInPostalCode)) { yield return(new ValidationResult("Please enter suitable Canadian Postal Code for your Province Code", new[] { nameof(PostalCode) })); yield return(new ValidationResult("Please enter suitable Province Code for your Canadian Postal Code", new[] { nameof(ProvinceCode) })); } else if (SBValidations.SBPostalCodeValidation(PostalCode)) { PostalCode = SBValidations.SBPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult("Please enter valid Canadian Postal Code", new[] { nameof(PostalCode) })); } } else { string postalCode = PostalCode; if (SBValidations.SBZipCodeValidation(ref postalCode)) { PostalCode = postalCode; } else { yield return(new ValidationResult("Please enter valid US Postal Code", new[] { nameof(PostalCode) })); } } } if (Ohip != null) { Ohip = Ohip.ToUpper(); var _ohipRegEx = @"^\d{4}-\d{3}-\d{3}-[A-Z]{2}$"; if (!Regex.Match(Ohip, _ohipRegEx).Success) { yield return(new ValidationResult("OHIP, if provided must match pattern; 1234-123-123-XX", new[] { nameof(Ohip) })); } } if (HomePhone != null) { HomePhone = SBValidations.SBExtractDigits(HomePhone); if (HomePhone.Length != 10) { yield return(new ValidationResult("Enter valid phone number", new[] { nameof(HomePhone) })); } HomePhone = string.Format("{0:###-###-####}", long.Parse(HomePhone)); } if (DateOfBirth != null || DateOfBirth.ToString().Trim() != "") { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("Date of birth can't be in the future", new[] { nameof(DateOfBirth) })); } } if (Deceased) { if (DateOfDeath == null || DateOfDeath.ToString().Trim() == "") { yield return(new ValidationResult("Date of death is required if the person is deceased", new[] { nameof(DateOfDeath) })); } else if (DateOfDeath.Value > DateTime.Now || DateOfDeath.Value < DateOfBirth) { yield return(new ValidationResult("Date of death can't be before date of birth or in the future", new[] { nameof(DateOfDeath) })); } } else { if (!(DateOfDeath == null) || !(DateOfDeath.ToString().Trim() == "")) { yield return(new ValidationResult("Date of death is not required", new[] { nameof(DateOfDeath) })); } } if (Gender == null || Gender.Trim() == "") { yield return(new ValidationResult("Gender cannot be empty or just blanks", new[] { nameof(Gender) })); } else { Gender = SBValidations.SBCapitaize(Gender).Substring(0, 1); if (!Gender.Equals("M") && !Gender.Equals("F") && !Gender.Equals("X")) { yield return(new ValidationResult("Please enter valid gender among (M, F, X)", new[] { nameof(Gender) })); } } if (Address != null) { if (Address.Trim() != "") { Address = SBValidations.SBCapitaize(Address); } } if (City != null) { if (City.Trim() != "") { City = SBValidations.SBCapitaize(City); } } yield return(ValidationResult.Success); }
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); }
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) { //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) { //First Name check and Capitalize if (string.IsNullOrEmpty(FirstName)) { yield return(new ValidationResult("Requir First Name", new[] { "FirstName" })); } else { FirstName = KYValidations.KYCapitalize(FirstName); } //Last Name Check and Capitalize if (string.IsNullOrEmpty(LastName)) { yield return(new ValidationResult("Requir Last Name", new[] { "LastName" })); } else { LastName = KYValidations.KYCapitalize(LastName); } // Address Capitalize Address = KYValidations.KYCapitalize(Address); // City Capitalize City = KYValidations.KYCapitalize(City); // Province Code Check Province findProvinceinDatadabase = null; if (!string.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.ToUpper(); string err = string.Empty; try { findProvinceinDatadabase = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); } catch (Exception e) { err = e.GetBaseException().Message; } if (findProvinceinDatadabase == null) { yield return(new ValidationResult("Province Code is not on file", new[] { "ProvinceCode" })); } if (!string.IsNullOrEmpty(err)) { yield return(new ValidationResult(err, new[] { "ProvinceCode" })); } } //postalcode check and format if (!string.IsNullOrEmpty(PostalCode)) { if (string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("Province Code is required to validate Postal Code", new[] { "ProvinceCode", "PostalCode" })); } if (findProvinceinDatadabase != null) { if (findProvinceinDatadabase.CountryCode == "CA") { //if (!PostalCode.StartsWith(findProvinceinDatadabase.FirstPostalLetter)) // yield return new ValidationResult("First letter of Postal Code not valid for given province", new[] { "ProvinceCode", "PostalCode", "" }); if (KYValidations.KYPostalCodeValidation(PostalCode)) { PostalCode = KYValidations.KYPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult("Postal Code not CDN pattern: A3A 3A3", new[] { "PostalCode" })); } } else if (findProvinceinDatadabase.CountryCode == "USA") { string localPostalCode = PostalCode; if (KYValidations.KYZipCodeValidation(ref localPostalCode)) { PostalCode = localPostalCode; } else { yield return(new ValidationResult("Zip Code is not valid", new[] { "PostalCode" })); } } } } //OHIP check if (!string.IsNullOrEmpty(Ohip)) { Ohip = Ohip.ToUpper().Trim(); Regex OhipValidate = new Regex(@"^\d{4}-\d{3}-\d{3}-[A-Z][A-Z]$"); if (!OhipValidate.IsMatch(Ohip)) { yield return(new ValidationResult("OHIP. if provided, must match pattern: 1234-123-123-XX", new[] { "Ohip" })); } } //Date of Birth check if (DateOfBirth != null) { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("Date of Birth cannot be in the future", new[] { "DateOfBirth" })); } } // Date of Death check string popupMessage = string.Empty; if (Deceased) { if (DateOfDeath == null) { popupMessage = "Deceased must be true if Date of Death is provided"; yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" })); } else if (DateOfDeath > DateTime.Now) { popupMessage = "Date of Death cannot be in the future"; yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" })); } else if (DateOfDeath < DateOfBirth) { popupMessage = "Date of Death cannot be before Date of Birth"; yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath", "DateOfBirth" })); } } else { if (DateOfDeath != null) { popupMessage = "Deceased must be true if Date of Death is provided"; yield return(new ValidationResult(popupMessage, new[] { "Deceased" })); } if (DateOfDeath != null && DateOfDeath > DateTime.Now) { popupMessage = "Date of Death cannot be in the future"; yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" })); } } //Phone check if (!string.IsNullOrEmpty(HomePhone)) { HomePhone = HomePhone.Trim(); HomePhone = KYValidations.KYExtractDigits(HomePhone); if (HomePhone.Length != 10) { yield return(new ValidationResult("Home Phone, if provided, must be 10 digits: 123-123-1234", new[] { "HomePhone" })); } else { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); } } //Gender Check and Capitalize if (string.IsNullOrEmpty(Gender)) { yield return(new ValidationResult("Requir Gender", new[] { "Gender" })); } else { string lowerGender = Gender.Trim().ToLower();; if (lowerGender != "m" && lowerGender != "f" && lowerGender != "x") { yield return(new ValidationResult("Gender must be either 'M', 'F' or 'X'", new[] { "Gender" })); } else { Gender = KYValidations.KYCapitalize(Gender); } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //trim all strings //DateOfDeath, DateOfBirth are not trimmed because they are not string //first name process if (string.IsNullOrEmpty(FirstName)) { yield return(new ValidationResult("First Name is required", new[] { "FirstName" })); } else { FirstName = FirstName.Trim(); FirstName = AYValidation.AYCapitalize(FirstName); } if (string.IsNullOrEmpty(LastName)) { yield return(new ValidationResult("last Name is required", new[] { "LastName" })); } else { LastName = LastName.Trim(); LastName = AYValidation.AYCapitalize(LastName); } Regex genderPattern = new Regex(@"^[MFXmfx]$"); if (string.IsNullOrEmpty(Gender)) { yield return(new ValidationResult("Gender is required", new[] { "Gender" })); } else if (genderPattern.IsMatch(Gender)) { Gender = AYValidation.AYCapitalize(Gender); } else { yield return(new ValidationResult("Gender is among: M, F, X", new[] { "Gender" })); } Address = AYValidation.AYCapitalize(Address).Trim(); City = AYValidation.AYCapitalize(City).Trim(); Province correspondingProvince = null; if (!string.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.Trim().ToUpper(); string err = string.Empty; try { correspondingProvince = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); } catch (Exception ex) { err = ex.GetBaseException().Message; } if (correspondingProvince == null) { yield return(new ValidationResult("Province Code not on the list", new[] { "ProvinceCode" })); } if (!string.IsNullOrEmpty(err)) { yield return(new ValidationResult(err, new[] { "ProvinceCode" })); } } if (!string.IsNullOrEmpty(PostalCode)) { if (string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("Province Code is needed first", new[] { "PostalCode" })); } if (correspondingProvince != null) //countrycode could be US or CA { if (correspondingProvince.CountryCode == "CA") { Regex firstLetterPattern = new Regex("^[" + correspondingProvince.FirstPostalLetter + "]$"); if (!firstLetterPattern.IsMatch(PostalCode.Substring(0, 1))) { yield return(new ValidationResult("Postal Code is not correct for that Province", new[] { "ProvinceCode", "PostalCode" })); } else { if (AYValidation.AYPostalCodeValidation(PostalCode)) { PostalCode = AYValidation.AYPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult("Postal Code is not correct.", new[] { "PostalCode" })); } } } } } if (!string.IsNullOrEmpty(Ohip)) { Ohip = Ohip.Trim().ToUpper(); Regex ohipPattern = new Regex(@"^\d\d\d\d[-]\d\d\d[-]\d\d\d[-][A-Z][A-Z]$", RegexOptions.IgnoreCase); if (!ohipPattern.IsMatch(Ohip)) { yield return(new ValidationResult("Ohip should match pattern: 1234-123-123-XX", new[] { "Ohip" })); } } if (!string.IsNullOrEmpty(HomePhone)) { HomePhone = AYValidation.AYExtractDigits(HomePhone); if (HomePhone.Length != 10) { yield return(new ValidationResult("Home phone should be 10 digits", new[] { "HomePhone" })); } else { HomePhone = HomePhone.Insert(3, "-").Insert(7, "-"); } } if (DateOfBirth != null) //dateofbirth is not string { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("Date of birth is not correct", new[] { "DateOfBirth" })); } } if (Deceased) { if (DateOfDeath == null) { yield return(new ValidationResult("Please enter date of death", new[] { "DateOfBirth" })); } if (DateOfDeath > DateTime.Now || DateOfDeath > DateTime.Now) { yield return(new ValidationResult("Please enter correct date of death", new[] { "DateOfBirth" })); } } else { if (DateOfDeath != null) { yield return(new ValidationResult("Don' enter date of death if not deceased", new[] { "DateOfDeath" })); } } yield return(ValidationResult.Success); }
/// <summary> /// Method that validades properties from Member Controller /// </summary> /// <param name="validationContext"></param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { #region global variables and database connection SailContext _context = SailContext_Singleton.Context(); #endregion var results = new List <ValidationResult>(); // Trim all string fields fields FirstName = KGClassLibrary.KGValidations.KGTrimFields(FirstName); LastName = KGClassLibrary.KGValidations.KGTrimFields(LastName); SpouseFirstName = KGClassLibrary.KGValidations.KGTrimFields(SpouseFirstName); SpouseLastName = KGClassLibrary.KGValidations.KGTrimFields(SpouseLastName); Street = KGClassLibrary.KGValidations.KGTrimFields(Street); City = KGClassLibrary.KGValidations.KGTrimFields(City); ProvinceCode = KGClassLibrary.KGValidations.KGTrimFields(ProvinceCode); PostalCode = KGClassLibrary.KGValidations.KGTrimFields(PostalCode); HomePhone = KGClassLibrary.KGValidations.KGTrimFields(HomePhone); Email = KGClassLibrary.KGValidations.KGTrimFields(Email); Comment = KGClassLibrary.KGValidations.KGTrimFields(Comment); // Capitalize FirstName = KGClassLibrary.KGValidations.KGCapitalize(FirstName); LastName = KGClassLibrary.KGValidations.KGCapitalize(LastName); SpouseFirstName = KGClassLibrary.KGValidations.KGCapitalize(SpouseFirstName); SpouseLastName = KGClassLibrary.KGValidations.KGCapitalize(SpouseLastName); Street = KGClassLibrary.KGValidations.KGCapitalize(Street); City = KGClassLibrary.KGValidations.KGCapitalize(City); ProvinceCode = ProvinceCode.ToUpper(); // member name is provided if ((LastName != null || LastName != "") && (FirstName != null || FirstName != "")) { // spouse name is not provided if ((SpouseFirstName == null || SpouseFirstName == "") && (SpouseLastName == null || SpouseLastName == "")) { FullName = LastName + ", " + FirstName; } // spouse name was provided else if ((SpouseFirstName != null || SpouseFirstName != "" && SpouseLastName != null || SpouseLastName != "")) { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } // spouse last name is not provided else if ((SpouseFirstName != null || SpouseFirstName != "") && (SpouseLastName == null || SpouseLastName == "") || (SpouseLastName == LastName)) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } } // Validate ProvinceCode Province province = null; string provinceError = null; try { province = _context.Province .Where(p => p.ProvinceCode == ProvinceCode) .FirstOrDefault(); }catch (Exception ex) { provinceError = ex.Message; } if (provinceError != null) { yield return(new ValidationResult(provinceError)); } // Member selected useCanadaPost if (UseCanadaPost) { if (province != null) { var country = _context.Country .Where(c => c.CountryCode == province.CountryCode) .FirstOrDefault(); // Country is CA if (country.CountryCode == "CA") { // Validate postal code if (string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult("Postal code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } else if (!KGClassLibrary.KGValidations.KGPostalCodeValidation(PostalCode)) { yield return(new ValidationResult("Postal code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } // Postal Code correct else { PostalCode = KGClassLibrary.KGValidations.KGPostalCodeFormat(PostalCode); } } // Country is US else if (country.CountryCode == "US") { string refPostalCode = PostalCode; // Validate Zip Code if (KGClassLibrary.KGValidations.KGZipCodeValidation(ref refPostalCode)) { yield return(new ValidationResult("Zip code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } else { PostalCode = KGClassLibrary.KGValidations.KGPostalCodeFormat(PostalCode); } } } else { yield return(new ValidationResult("Province Code " + ProvinceCode + " is not on file", new[] { nameof(ProvinceCode) })); } } //useCanadaPost is false else { // If postalCode is provided, Provincecode cannot be null if (PostalCode != null && string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("PostalCode is optional but, if provided, provinceCode becomes required", new[] { nameof(ProvinceCode) })); } else // If postal Code inserted it is validated if (!KGClassLibrary.KGValidations.KGPostalCodeValidation(PostalCode)) { yield return(new ValidationResult("Postal code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } // if provinceCode exists on province table if (province != null) { var country = _context.Country .Where(c => c.CountryCode == province.CountryCode) .FirstOrDefault(); // Country is Canada if (country.CountryCode == "CA") { // Validate postalCode if (string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult("Postal code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } else if (!KGClassLibrary.KGValidations.KGPostalCodeValidation(PostalCode)) { yield return(new ValidationResult("Postal code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } // Format postal code if no error else { PostalCode = KGClassLibrary.KGValidations.KGPostalCodeFormat(PostalCode); } } // country is US else if (country.CountryCode == "US") { string refPostalCode = PostalCode; // Validate Zip Code if (!KGClassLibrary.KGValidations.KGZipCodeValidation(ref refPostalCode)) { yield return(new ValidationResult("Zip code " + PostalCode + " not valid", new[] { nameof(PostalCode) })); } // // Format zip code if no error else { PostalCode = refPostalCode; PostalCode = KGClassLibrary.KGValidations.KGPostalCodeFormat(PostalCode); } } } } // if province Code is empty set it to null, because database does not acept empty if (ProvinceCode == "") { ProvinceCode = null; } // Validate HomePhone HomePhone = KGClassLibrary.KGValidations.KGExtractDigits(HomePhone); if (HomePhone.Length != 10) { yield return(new ValidationResult("Home phone " + HomePhone + " must be 10 digits long: 519-748-5220", new[] { nameof(HomePhone) })); } else { // Format to insert dashes HomePhone = string.Format("{0:###-###-####}", Convert.ToSingle(HomePhone)); } // Validate email, only if it is provided if (!string.IsNullOrEmpty(Email)) { Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); if (!regex.IsMatch(Email)) { yield return(new ValidationResult("Email " + Email + " is in a wrong format, please follow [email protected]", new[] { nameof(Email) })); } } // If use CanadaPost is not selected, email is required if (UseCanadaPost == false) { Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); if (!regex.IsMatch(Email)) { yield return(new ValidationResult("Email " + Email + " is required unless member has restricted communication to Canada Post", new[] { nameof(Email) })); } } else { // usePostalcode is true, Street Address is required if (string.IsNullOrEmpty(Street)) { yield return(new ValidationResult("Member wants to use Cananda Post - Street Address is required", new[] { nameof(Street) })); } // usePostalcode is true, city is required if (string.IsNullOrEmpty(City)) { yield return(new ValidationResult("Member wants to use Cananda Post - City is required", new[] { nameof(City) })); } } // Validate Year Joined if (YearJoined != null) { if (YearJoined > DateTime.Today.Year) { yield return(new ValidationResult("The Year the member Joined the club cannot be in the future", new[] { nameof(YearJoined) })); } } else { // When creating a new member, year joined is required if (MemberId == 0) { yield return(new ValidationResult("The Year the member Joined the club cannot be null", new[] { nameof(YearJoined) })); } } // if no error yield return(ValidationResult.Success); }
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) { if (string.IsNullOrEmpty(FirstName)) { yield return(new ValidationResult("First name cannot be empty")); } if (string.IsNullOrEmpty(LastName)) { yield return(new ValidationResult("Last Name cannot be empty", new[] { nameof(LastName) })); } if (FirstName != null) { FirstName = FirstName.Trim(); } if (LastName != null) { LastName = LastName.Trim(); } if (Address != null) { Address = Address.Trim(); } if (City != null) { City = City.Trim(); } if (ProvinceCode != null) { ProvinceCode = ProvinceCode.ToUpper(); } if (PostalCode != null) { PostalCode = PostalCode.ToUpper(); } if (DateOfBirth != null) { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("DateOfBirth can not be in future")); } if (DateOfDeath != null) { if (DateOfBirth > DateOfDeath) { yield return(new ValidationResult("DateOfDeath can not be before DateOfBirth")); } if (DateOfDeath > DateTime.Now) { yield return(new ValidationResult("DateOfDeath can not be in future")); } if (Deceased == false) { yield return(new ValidationResult("Deceased must be checked ")); } } } if (HomePhone != null) { if (HomePhone.Length != 10) { yield return(new ValidationResult("Homephone numbe rshould be exactly 10 numbers")); } else { HomePhone = Regex.Replace(HomePhone.ToString(), "[^0-9]+", string.Empty); HomePhone = HomePhone.ToString().Insert(3, "-").Insert(7, "-"); } } yield return(ValidationResult.Success); }
/// <summary> /// 身份证号码。 /// </summary> /// <returns></returns> public override string ToString() { return(string.Concat(ProvinceCode.ToString("00"), CityCode.ToString("00"), County.ToString("00"), Birthday.ToString("yyyyMMdd"), SerialNumber.ToString("000"), CheckCode)); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { string countryCodeFromInput = ""; // 2. b. Trim all strings of leading and trailing spaces (fields not trimmed here // are trimmed by TACapitalize and TAExtractDigits methods below) if (ProvinceCode != null) { ProvinceCode = ProvinceCode.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } // 2.d. Using our library method to capitalize names, address and city FirstName = ISValidations.ISCapitalize(FirstName); LastName = ISValidations.ISCapitalize(LastName); SpouseFirstName = ISValidations.ISCapitalize(SpouseFirstName); SpouseLastName = ISValidations.ISCapitalize(SpouseLastName); Street = ISValidations.ISCapitalize(Street); City = ISValidations.ISCapitalize(City); // 2.e. Compiling the full name from member's last and first name and // their spouse's names if (SpouseLastName == "" && SpouseFirstName == "") { FullName = LastName + ", " + FirstName; } else { if (SpouseLastName == "") { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else { if (SpouseLastName == LastName) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } } if (SpouseFirstName == "") { FullName = LastName + ", " + FirstName + " & " + SpouseLastName; } } // 2.f. Force the province code to upper cases and validate it if (ProvinceCode != null && ProvinceCode != "") { string errors = ""; Province province = new Province(); ProvinceCode = ProvinceCode.ToUpper(); // we try to fetch a province record with the same province code from database try { province = _context.Province.FirstOrDefault(p => p.ProvinceCode == ProvinceCode); } catch (Exception ex) { errors = ex.GetBaseException().Message; } // if fetching province code throws an exception, put its innermost message in ModelState if (errors != "") { yield return(new ValidationResult(errors, new[] { "ProvinceCode" })); } // if it's not on the file, display an appropriate message if (province == null) { yield return(new ValidationResult("This province code is not on the file", new[] { "ProvinceCode" })); } else { // 2.f.iii. Retain a record's Country field to use it in the postal code validation countryCodeFromInput = province.CountryCode; } // 2.g.i. Validate and format the postal/zip code depending on the country of origin if (countryCodeFromInput == "CA") { if (ISValidations.ISPostalCodeValidation(PostalCode)) { PostalCode = ISValidations.ISPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult("The postal code must be in format A1A 1A1", new[] { "PostalCode" })); } } else if (countryCodeFromInput == "US") { string postalCode = PostalCode; if (!ISValidations.ISZipCodeValidation(ref postalCode)) { yield return(new ValidationResult("This zip code must contain 5 or 9 digits", new[] { "PostalCode" })); } else { PostalCode = postalCode; } } } // 2.g. If the postal code is provided, but the province code isn't, throw an error else { if (PostalCode != null && PostalCode != string.Empty) { yield return(new ValidationResult("The province/state code is required to validate the " + "postal/zip code", new[] { "ProvinceCode" })); } } // 2.h.i. Home phone must contain 10 digits HomePhone = ISValidations.ISExtractDigits(HomePhone); if (HomePhone.Length != 10) { yield return(new ValidationResult("The phone number must be 10 digits long", new[] { "HomePhone" })); } else { // 2.h.ii. If it does, reformat it into dash notation HomePhone = HomePhone.Substring(0, 3) + "-" + HomePhone.Substring(3, 3) + "-" + HomePhone.Substring(6); } // Email address must be a valid email pattern if (!(new EmailAddressAttribute()).IsValid(Email)) { yield return(new ValidationResult("The Email address is in incorrect format ", new[] { "Email" })); } // 2.j. Year Joined can only be null when editing an existing record if (MemberId == 0 && YearJoined == null) { yield return(new ValidationResult("Year Joined cannot be empty for a new " + "record", new[] { "YearJoined" })); } // 2.j. Year Joined cannot be in the future if (YearJoined != null) { if (YearJoined > DateTime.Now.Year) { yield return(new ValidationResult("The year the member joined the club cannot " + "be in the future", new[] { "YearJoined" })); } } // 2.k. default taskExepmpt and useCanadaPost to false if (TaskExempt == null) { TaskExempt = false; } if (UseCanadaPost == null) { UseCanadaPost = false; } // 2.l.i If useCanadaPost is false, valid email is required if (UseCanadaPost == false) { if (Email == null || Email == "") { yield return(new ValidationResult("Email address is required unless member has " + "restricted communication to Canada Post", new[] { "Email" })); } } // 2.l.ii. If useCanadaPost is true, all the postal fields are required else { if (Street == null || Street == "") { yield return(new ValidationResult("Member wants to use Canada Post - Street " + "Address is required", new[] { "Street" })); } if (City == null || City == "") { yield return(new ValidationResult("Member wants to use Canada Post - City/Town " + "is required", new[] { "City" })); } if (ProvinceCode == null || ProvinceCode == "") { yield return(new ValidationResult("Member wants to use Canada Post - Province " + "Code is required", new[] { "ProvinceCode" })); } if (PostalCode == null || PostalCode == "") { yield return(new ValidationResult("Member wants to use Canada Post - Postal " + "Code is required", new[] { "PostalCode" })); } } 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) { //Check if the provinceCode already exists int the database var checkProvinceCode = _context.Province .Where(p => p.ProvinceCode == ProvinceCode) .Select(p => p.ProvinceCode) .SingleOrDefault(); if (ProvinceCode != null) { if (checkProvinceCode != null) { yield return(new ValidationResult("Province already exists in the database", new[] { nameof(ProvinceCode) })); } else { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("Province should be exactly 2 letters ", new[] { nameof(ProvinceCode) })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } //check ProvinceNmae already exists in the database var checkProvinceNmae = _context.Province .Where(p => p.Name == Name) .Select(p => p.Name) .SingleOrDefault(); TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; if (Name == null || Name.Trim() == "" || Name == "") { yield return(new ValidationResult("Name can not be empty ", new[] { nameof(Name) })); } else { if (checkProvinceNmae != null) { yield return(new ValidationResult("Province name already exists in the database", new[] { nameof(Name) })); } else { Name = textInfo.ToTitleCase(Name.ToLower().Trim()); } } //check if CountryCode exists in the database var checkCountryCode = _context.Country .Where(c => c.CountryCode == CountryCode) .Select(c => c.CountryCode) .SingleOrDefault(); if (CountryCode == null) { yield return(new ValidationResult("CountryCode can not be empty", new[] { nameof(Name) })); } else { if (checkCountryCode == null) { yield return(new ValidationResult("CountryCode does not exist in the Country table", new[] { nameof(Name) })); } else { CountryCode = CountryCode.ToUpper(); } } if (TaxCode != null) { TaxCode = TaxCode.ToUpper(); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (FirstName.Trim().Length == 0) { yield return(new ValidationResult("FirstName cannot be empty or blank ", new[] { "FirstName" })); } if (LastName.Trim().Length == 0) { yield return(new ValidationResult("FirstName cannot be empty or blank ", new[] { "LastName" })); } if (UseCanadaPost == true) { if (string.IsNullOrEmpty(Street)) { yield return(new ValidationResult("Street is Required ", new[] { "Street" })); } if (string.IsNullOrEmpty(City)) { yield return(new ValidationResult("City is Required ", new[] { "City" })); } Street = Street.Trim(); City = City.Trim(); } if (UseCanadaPost == false) { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("Email is Required ", new[] { "Email" })); } else { Email = Email.Trim(); } } // MemberId = _context.Member.Max(a => a.MemberId) + 1; //var memberid = _context.Member.Where(a => a.MemberId == MemberId).FirstOrDefault(); //if (memberid != null) //{ // yield return new ValidationResult("Member is already on file", new[] { "MemberId" }); //} ProvinceCode = ProvinceCode.Trim(); ProvinceCode = ProvinceCode.ToUpper(); if (ProvinceCode.Length != 2) { yield return(new ValidationResult("ProvinceCode length is not match ", new[] { "ProvinceCode" })); } var province = _context.Member.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); if (province == null) { yield return(new ValidationResult("ProvinceCode is not match ", new[] { "ProvinceCode" })); } if (!string.IsNullOrEmpty(PostalCode)) { PostalCode = PostalCode.ToUpper().Trim(); Regex PostalValidation = new Regex(@"^(?!.*[DFIOQU])[A-VXY][0-9][A-Z][0-9][A-Z][0-9]$"); if (!PostalValidation.IsMatch(PostalCode)) { yield return(new ValidationResult("PostalCode is not match ", new[] { "PostalCode" })); } else { PostalCode = PostalCode.Insert(3, " "); } } if (!string.IsNullOrEmpty(HomePhone)) { HomePhone = HomePhone.Trim(); Regex PhoneValidation = new Regex(@"^\(?[0-9]{3}(\-|\)) ?[0-9]{3}-[0-9]{4}$"); if (HomePhone.Length != 10 && !PhoneValidation.IsMatch(HomePhone)) { yield return(new ValidationResult("Home Phone should only be 10 digits", new[] { "HomePhone" })); } else { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); } } if (!string.IsNullOrEmpty(SpouseFirstName)) { if (!string.IsNullOrEmpty(SpouseLastName)) { SpouseFirstName = SpouseFirstName.Trim(); SpouseLastName = SpouseLastName.Trim(); FullName = LastName + ", " + FirstName + "&" + SpouseLastName + ", " + SpouseFirstName; } else { FullName = LastName + ", " + FirstName + "&" + SpouseFirstName; } } else { FirstName = FirstName.Trim(); LastName = LastName.Trim(); FullName = LastName + ", " + FirstName; } }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //throw new NotImplementedException(); if (FirstName == null || FirstName.Trim() == "") { yield return(new ValidationResult("First name cannot be empty or just blanks", new[] { nameof(FirstName) })); } if (!(FirstName == null || FirstName.Trim() == "")) { FirstName = FirstName.Trim(); } if (LastName == null || LastName.Trim() == "") { yield return(new ValidationResult("Last name cannot be empty or just blanks", new[] { nameof(LastName) })); } if (!(LastName == null || LastName.Trim() == "")) { LastName = LastName.Trim(); } FirstName = KAValidations.KACapitalize(FirstName); LastName = KAValidations.KACapitalize(LastName); Address = KAValidations.KACapitalize(Address); City = KAValidations.KACapitalize(City); Gender = KAValidations.KACapitalize(Gender); if (!String.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.ToUpper(); } if (!String.IsNullOrEmpty(Ohip)) { Ohip = KAValidations.KACapitalize(Ohip); if (!KAValidations.KAOhipValidation(Ohip)) { yield return(new ValidationResult("Ohip format not accepted. Accepted format is 1234-123-123-XX", new[] { nameof(Ohip) })); } } if (!String.IsNullOrEmpty(HomePhone)) { string phoneDigits = KAValidations.KAExtractDigits(HomePhone); if (Regex.IsMatch(phoneDigits, @"^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$")) { phoneDigits = phoneDigits.Insert(3, "-"); phoneDigits = phoneDigits.Insert(7, "-"); HomePhone = phoneDigits; } else { yield return(new ValidationResult("Home Phone number format is wrong. Home phone number must contain exactly 10 digits", new[] { nameof(HomePhone) })); } } if (!String.IsNullOrEmpty(DateOfBirth.ToString())) { if (DateOfBirth > DateTime.Now) { yield return(new ValidationResult("Date of birth cannot be in future", new[] { nameof(DateOfBirth) })); } } if (Deceased) { if (String.IsNullOrWhiteSpace(DateOfDeath.ToString())) { yield return(new ValidationResult("Date of Death is compulsary for deceased patients", new[] { nameof(DateOfDeath) })); } } if (!Deceased) { if (!String.IsNullOrWhiteSpace(DateOfDeath.ToString())) { yield return(new ValidationResult("Cannot add date of death for patient who is not dead (deceased must be checked)", new[] { nameof(DateOfDeath) })); } } if (!String.IsNullOrWhiteSpace(DateOfDeath.ToString())) { if (DateOfDeath < DateOfBirth) { yield return(new ValidationResult("Date of death cannot be before date of birth.", new[] { nameof(DateOfDeath) })); } if (DateOfDeath > DateTime.Now) { yield return(new ValidationResult("Date of death cannot be in future", new[] { nameof(DateOfDeath) })); } } if (String.IsNullOrWhiteSpace(Gender)) { yield return(new ValidationResult("Gender is required", new[] { nameof(Gender) })); } if (!(Gender == "M" || Gender == "F" || Gender == "X")) { yield return(new ValidationResult("Gender must be M or F or X", new[] { nameof(Gender) })); } localPatientContext = (PatientsContext)validationContext.GetService(typeof(PatientsContext)); if (!String.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = KAValidations.KACapitalize(ProvinceCode); //try //{ if (!(localPatientContext.Province.Any(x => x.ProvinceCode == ProvinceCode))) { yield return(new ValidationResult("This province name do not exists", new[] { nameof(ProvinceCode) })); } //} //catch (Exception ex) { //} } if (!String.IsNullOrEmpty(PostalCode)) { if (String.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("In order to add/edit postal code, You need to add valid Province code ", new[] { nameof(PostalCode), nameof(ProvinceCode) })); } PostalCode = PostalCode.ToUpper(); firstLetterPostalCode = PostalCode[0].ToString(); } if (localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode) .Select(x => x.CountryCode).FirstOrDefault() == "CA") { if (ProvinceCode.ToUpper() == "ON") { if (!(firstLetterPostalCode == "K" || firstLetterPostalCode == "L" || firstLetterPostalCode == "M" || firstLetterPostalCode == "N" || firstLetterPostalCode == "P")) { yield return(new ValidationResult("Postal Code and Province Name are not matching ", new[] { nameof(PostalCode), nameof(ProvinceCode) })); } } else if (ProvinceCode.ToUpper() == "QC") { if (!(firstLetterPostalCode == "G" || firstLetterPostalCode == "H" || firstLetterPostalCode == "J")) { yield return(new ValidationResult("Postal Code and Province Name are not matching ", new[] { nameof(PostalCode), nameof(ProvinceCode) })); } } else { if (!(localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode) .Select(x => x.FirstPostalLetter).FirstOrDefault() == firstLetterPostalCode)) { yield return(new ValidationResult("Postal Code and Province Name are not matching ", new[] { nameof(PostalCode), nameof(ProvinceCode) })); } } if (!KAValidations.KAPostalCodeValidation(PostalCode)) { yield return(new ValidationResult("The postal code is not as per the format", new[] { nameof(PostalCode) })); } if (KAValidations.KAPostalCodeValidation(PostalCode)) { PostalCode = KAValidations.KAPostalCodeFormat(PostalCode); } } if (localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode) .Select(x => x.CountryCode).FirstOrDefault() == "US") { if (!KAValidations.KAZipCodeValidation(PostalCode)) { yield return(new ValidationResult("The ZIP code is not as per the format", new[] { nameof(PostalCode) })); } } yield return(ValidationResult.Success); }
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) })); } } }
//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) { _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); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { Province province = new Province(); string PostalCodePattern = ""; string phonePattern = ""; string CapitalFirst(string line) { string[] toSplit; toSplit = line.Split(); line = ""; foreach (var item in toSplit) { line += item.Substring(0, 1).ToUpper() + item.Substring(1).ToLower(); } return(line.Trim()); } if (!string.IsNullOrEmpty(FirstName)) { FirstName = FirstName.Trim(); FirstName = CapitalFirst(FirstName); } else { yield return(new ValidationResult("first name cant be empty", new[] { "FirstName" })); } if (!string.IsNullOrEmpty(LastName)) { LastName = CapitalFirst(LastName).Trim(); } else { yield return(new ValidationResult("last name cant be empty", new[] { "lastName" })); } if (!string.IsNullOrEmpty(City)) { City = City.Trim(); } if (!string.IsNullOrEmpty(Address)) { Address = Address.Trim(); } if (!string.IsNullOrEmpty(ProvinceCode)) { ProvinceCode = ProvinceCode.Trim().ToUpper(); province = _context.Province.FirstOrDefault(p => p.ProvinceCode == ProvinceCode); if (province == null) { yield return(new ValidationResult("ProvinceCode is not in file", new[] { "ProvinceCode" })); } else { Country country = _context.Country .FirstOrDefault(c => c.CountryCode == province.CountryCode); PostalCodePattern = country.PostalPattern; phonePattern = country.PhonePattern; } } if (string.IsNullOrEmpty(ProvinceCode) && !string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult("you need to enter province code to validate", new[] { "PostalCode" })); } else if (!string.IsNullOrEmpty(PostalCode)) { Regex regex = new Regex(PostalCodePattern, RegexOptions.IgnoreCase); if (!regex.IsMatch(PostalCode)) { yield return(new ValidationResult(" provincecode is not match", new[] { "PostalCode" })); } } yield return(ValidationResult.Success); }