public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (FirstName == null || FirstName.Trim() == "") { yield return(new ValidationResult("First name cannot be empty or just blanks", new[] { nameof(FirstName) })); } else { FirstName = FirstName.Trim(); } //FullName = FullName.Trim(); LastName.Trim(); FirstName = JRValidations.JRCapitalize(FirstName.Trim()); //FullName = JRValidations.JRCapitalize(FullName.Trim()); LastName = JRValidations.JRCapitalize(LastName.Trim()); LastName = JRValidations.JRCapitalize(SpouseFirstName.Trim()); LastName = JRValidations.JRCapitalize(SpouseLastName.Trim()); LastName = JRValidations.JRCapitalize(Street.Trim()); LastName = JRValidations.JRCapitalize(City.Trim()); //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) { // first name FirstName = FirstName.Trim(); FirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(FirstName); // last name LastName = LastName.Trim(); LastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(LastName); // spouse first name SpouseFirstName = SpouseFirstName.Trim(); SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName); // spouse last name SpouseLastName = SpouseLastName.Trim(); SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName); if (String.IsNullOrEmpty(SpouseFirstName)) { SpouseFirstName = null; } else { SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName.Trim()); } if (String.IsNullOrEmpty(SpouseLastName)) { SpouseLastName = null; } else { SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName.Trim()); } // street Street = Street.Trim(); Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street); if (String.IsNullOrEmpty(Street)) { Street = null; } else { Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street.Trim()); } // city City = City.Trim(); City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City); if (String.IsNullOrEmpty(City)) { City = null; } else { City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City.Trim()); } // postal code if (PostalCode == null || PostalCode == "") { PostalCode = ""; } else { PostalCode = PostalCode.Trim(); if (HMBPClassLibrary.HMBPValidations.HMBPPostalCodeValidation(PostalCode)) { PostalCode = HMBPClassLibrary.HMBPValidations.HMBPPostalCodeFormat(PostalCode); } else { string _postalCode = ""; _postalCode = PostalCode; if (HMBPClassLibrary.HMBPValidations.HMBPZipCodeValidation(ref _postalCode)) { PostalCode = _postalCode; } else { yield return(new ValidationResult("error", new[] { "PostalCode" })); } } if (ProvinceCode == null) { yield return(new ValidationResult("error", new[] { "ProvinceCode" })); } } // email if (string.IsNullOrEmpty(Email)) { Email = null; } else // trim { Email = Email.Trim(); } // comment if (string.IsNullOrEmpty(Comment)) { Comment = null; } else // trim { Comment = Comment.Trim(); } //home phone reformat HomePhone = HMBPClassLibrary.HMBPValidations.HMBPExtractDigits(HomePhone.Trim()); if (HomePhone.Length != 10) { yield return(new ValidationResult("The home phone can only contain 10 digits", new[] { nameof(HomePhone) })); } else { HomePhone = HomePhone.Insert(3, "-").Insert(7, "-"); } //validate Joined year if (YearJoined.HasValue) { if (YearJoined > DateTime.Now.Year) { YearJoined = null; yield return(new ValidationResult("The year cant be in the future", new[] { nameof(YearJoined) })); } } //Full name if (String.IsNullOrEmpty(SpouseFirstName) && String.IsNullOrEmpty(SpouseLastName)) { FullName = LastName + ", " + FirstName; } else if (!String.IsNullOrEmpty(SpouseFirstName)) { if (String.IsNullOrEmpty(SpouseLastName) || SpouseLastName == LastName) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else if (!String.IsNullOrEmpty(SpouseLastName)) { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } } yield return(ValidationResult.Success); }
//self_validation for memebr class public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { SailContext _context = new SailContext(); //trim all strings FirstName = FirstName.Trim(); LastName = LastName.Trim(); if (SpouseFirstName != null) { SpouseFirstName = SpouseFirstName.Trim(); } if (SpouseLastName != null) { SpouseLastName = SpouseLastName.Trim(); } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (ProvinceCode != null) { ProvinceCode = ProvinceCode.Trim(); } if (PostalCode != null) { PostalCode = PostalCode.Trim(); } HomePhone = HomePhone.Trim(); if (Email != null) { Email = Email.Trim(); } //capitalize designated fields FirstName = HXClassLibrary.HXValidations.HXCapitalize(FirstName); LastName = HXClassLibrary.HXValidations.HXCapitalize(LastName); SpouseFirstName = HXClassLibrary.HXValidations.HXCapitalize(SpouseFirstName); SpouseLastName = HXClassLibrary.HXValidations.HXCapitalize(SpouseLastName); Street = HXClassLibrary.HXValidations.HXCapitalize(Street); City = HXClassLibrary.HXValidations.HXCapitalize(City); // format full name if (SpouseFirstName == "") { FullName = LastName + ", " + FirstName; } else if (SpouseFirstName != "" && SpouseLastName != "") { if (SpouseLastName == LastName) { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } else { FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName; } } else if (SpouseFirstName != "" && SpouseLastName == "") { FullName = LastName + ", " + FirstName + " & " + SpouseFirstName; } //validate province code Province province = null; string errorMessage = ""; try { ProvinceCode = (ProvinceCode + "").ToUpper(); province = _context.Province.Find(ProvinceCode); if (province == null) { errorMessage = "the province code is not on file"; } } catch (Exception ex) { errorMessage = $"fetching provinceCode error: {ex.GetBaseException().Message}"; } if (errorMessage != "") { yield return(new ValidationResult( errorMessage, new[] { "ProvinceCode" })); } //validate postal code if (!string.IsNullOrEmpty(PostalCode)) { if (string.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult( "province code is required when having the postal code", new[] { "ProvinceCode" })); } else { if (province == null) { yield return(new ValidationResult( "The province code is not on file", new[] { "ProvinceCode" })); } else { if (province.CountryCode == "CA") { if (HXClassLibrary.HXValidations.HXPostalCodeValidation(PostalCode)) { HXClassLibrary.HXValidations.HXPostalCodeFormat(PostalCode); } else { yield return(new ValidationResult( "the postal code is invalid in Canada (it should follow the format: A1B 1E1)", new[] { "PostalCode" })); } } else if (province.CountryCode == "US") { string postalCode = PostalCode; if (HXClassLibrary.HXValidations.HXZipCodeValidation(ref postalCode)) { PostalCode = postalCode; } else { yield return(new ValidationResult( "the zip code is invalid in the US (it should have 5 or 9 digits)", new[] { "PostalCode" })); } } } } } //validate home phone if (HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Length == 10) { HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Insert(3, "-").Insert(6, "-"); } else { HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone); yield return(new ValidationResult( "a valid phone number should have 10 digits", new[] { "HomePhone" })); } //validate year joined if (YearJoined > Convert.ToInt32(DateTime.Now.Year)) { yield return(new ValidationResult( "year joined can not be in the future", new[] { "YearJoined" })); } var memberId = _context.Member.Find(MemberId); if (memberId == null) { if (YearJoined == null) { yield return(new ValidationResult( "year joined can only be null for existing records.", new[] { "YearJoined" })); } } //validate using canada post if (UseCanadaPost == false) { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult( "Must have a valid email if not using Canada Post", new[] { "Email" })); } } else { if (string.IsNullOrEmpty(Street) || string.IsNullOrEmpty(City) || string.IsNullOrEmpty(ProvinceCode) || string.IsNullOrEmpty(PostalCode)) { yield return(new ValidationResult( "street, city, province code, postal code can not be null when using Canada Post.", new[] { "UseCanadaPost" })); } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("Province code cannot be longer than 2 characters", new[] { "ProvinceCode" })); } else { var provinceContext = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); if (provinceContext == null) { yield return(new ValidationResult("Invalid province code", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex postalCodeRegex = new Regex((@"^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}\s{0,1}[0-9]{1}[A-Za-z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCode != null) { if (postalCodeRegex.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("Invalid postal code. Postal code must match Canadian postal pattern", new[] { "PostalCode" })); } } Regex homePhoneRegex = new Regex(@"^[0-9]{3}-{0,1}[0-9]{3}-{0,1}[0-9]{4}"); if (HomePhone != null) { if (homePhoneRegex.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("Invalid Phone Number Format. It must be in the format : 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + ", " + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseLastName.Trim() + ", " + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street)) { yield return(new ValidationResult("If Canada post is checked, street name is required", new[] { "Street" })); } if (string.IsNullOrEmpty(City)) { yield return(new ValidationResult("If Canada post is checked, city name is required", new[] { "City" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("If Canada post is not checked, email address is required", new[] { "Email" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } if (LastName != null) { LastName = LastName.Trim(); } if (FirstName != null) { FirstName = FirstName.Trim(); } if (SpouseFirstName != null) { SpouseFirstName = SpouseFirstName.Trim(); } if (SpouseLastName != null) { SpouseLastName = SpouseLastName.Trim(); } //determine if editing or creating new var memberId = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (memberId != null) { //yield error : member id is already on file } else { //yield error: member id not on file } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //if (MemberId != 0) //{ // FullName = FirstName + ' ' + LastName + " & " + SpouseFirstName; //} if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("The Province Code should be exactly 2 characters long", new[] { "ProvinceCode" })); } else { var province = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault(); if (province == null) { yield return(new ValidationResult("The Province Code is not valid", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex PostalCodePattern = new Regex((@"^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s{0,1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCodePattern.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("The Postal Code entered is not in valid canadian format", new[] { "PostalCode" })); } Regex HomePhonePattern = new Regex(@"^\d\d\d-{0,1}\d\d\d-{0,1}\d\d\d\d"); if (HomePhone != null) { if (HomePhonePattern.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("The home Phone number entered is not in valid format 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + "," + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = FirstName.Trim() + ' ' + LastName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + "," + FirstName.Trim() + " & " + SpouseLastName.Trim() + "," + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street) && string.IsNullOrEmpty(City)) { yield return(new ValidationResult("The Street name and City Name field are required fields if you have checked Canada Post checkbox", new[] { "Street" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("The Email address field is required", new[] { "Email" })); } } if (MemberId == 0) { var duplicateID = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (duplicateID != null) { yield return(new ValidationResult("The Member Id entered is already on file", new[] { "MemberId" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } yield return(ValidationResult.Success); }