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); }
private void formatPostalCode() { PostalCode.ToUpper(); if (PostalCode.Length > 6) { PostalCode.Replace('-', ' '); } else { PostalCode.Insert(3, " "); } }
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) { _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) { 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) { if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("Province code cannot be longer than 2 characters", new[] { "ProvinceCode" })); } else { var provinceContext = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault(); if (provinceContext == null) { yield return(new ValidationResult("Invalid province code", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex postalCodeRegex = new Regex((@"^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}\s{0,1}[0-9]{1}[A-Za-z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCode != null) { if (postalCodeRegex.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("Invalid postal code. Postal code must match Canadian postal pattern", new[] { "PostalCode" })); } } Regex homePhoneRegex = new Regex(@"^[0-9]{3}-{0,1}[0-9]{3}-{0,1}[0-9]{4}"); if (HomePhone != null) { if (homePhoneRegex.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("Invalid Phone Number Format. It must be in the format : 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + ", " + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseLastName.Trim() + ", " + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street)) { yield return(new ValidationResult("If Canada post is checked, street name is required", new[] { "Street" })); } if (string.IsNullOrEmpty(City)) { yield return(new ValidationResult("If Canada post is checked, city name is required", new[] { "City" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("If Canada post is not checked, email address is required", new[] { "Email" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } if (LastName != null) { LastName = LastName.Trim(); } if (FirstName != null) { FirstName = FirstName.Trim(); } if (SpouseFirstName != null) { SpouseFirstName = SpouseFirstName.Trim(); } if (SpouseLastName != null) { SpouseLastName = SpouseLastName.Trim(); } //determine if editing or creating new var memberId = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (memberId != null) { //yield error : member id is already on file } else { //yield error: member id not on file } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //if (MemberId != 0) //{ // FullName = FirstName + ' ' + LastName + " & " + SpouseFirstName; //} if (ProvinceCode != null) { if (ProvinceCode.Length != 2) { yield return(new ValidationResult("The Province Code should be exactly 2 characters long", new[] { "ProvinceCode" })); } else { var province = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault(); if (province == null) { yield return(new ValidationResult("The Province Code is not valid", new[] { "ProvinceCode" })); } else { ProvinceCode = ProvinceCode.Trim().ToUpper(); } } } Regex PostalCodePattern = new Regex((@"^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s{0,1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}"), RegexOptions.IgnoreCase); if (PostalCodePattern.IsMatch(PostalCode.Trim())) { if (!PostalCode.Contains(" ")) { PostalCode = PostalCode.Insert(3, " "); PostalCode = PostalCode.Trim().ToUpper(); } else { PostalCode = PostalCode.Trim().ToUpper(); } } else { yield return(new ValidationResult("The Postal Code entered is not in valid canadian format", new[] { "PostalCode" })); } Regex HomePhonePattern = new Regex(@"^\d\d\d-{0,1}\d\d\d-{0,1}\d\d\d\d"); if (HomePhone != null) { if (HomePhonePattern.IsMatch(HomePhone)) { if (!HomePhone.Contains('-')) { HomePhone = HomePhone.Insert(3, "-"); HomePhone = HomePhone.Insert(7, "-"); HomePhone = HomePhone.Trim(); } } else { yield return(new ValidationResult("The home Phone number entered is not in valid format 999-999-9999", new[] { "HomePhone" })); } } if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName)) { FullName = LastName.Trim() + "," + FirstName.Trim(); } else { if (SpouseLastName == null || SpouseLastName == LastName) { FullName = FirstName.Trim() + ' ' + LastName.Trim() + " & " + SpouseFirstName.Trim(); } else { FullName = LastName.Trim() + "," + FirstName.Trim() + " & " + SpouseLastName.Trim() + "," + SpouseFirstName.Trim(); } } if (UseCanadaPost) { if (string.IsNullOrEmpty(Street) && string.IsNullOrEmpty(City)) { yield return(new ValidationResult("The Street name and City Name field are required fields if you have checked Canada Post checkbox", new[] { "Street" })); } } else { if (string.IsNullOrEmpty(Email)) { yield return(new ValidationResult("The Email address field is required", new[] { "Email" })); } } if (MemberId == 0) { var duplicateID = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault(); if (duplicateID != null) { yield return(new ValidationResult("The Member Id entered is already on file", new[] { "MemberId" })); } } if (Street != null) { Street = Street.Trim(); } if (City != null) { City = City.Trim(); } if (Email != null) { Email = Email.Trim(); } if (Comment != null) { Comment = Comment.Trim(); } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { //Use your XXStringManipulation.Capitalize method to capitalise FirstName, LastName, CompanyName, StreetAddress, and City. if (String.IsNullOrEmpty(FirstName)) { FirstName = ""; } else { FirstName = ERStringManipulation.ERCapitalize(FirstName); } if (String.IsNullOrEmpty(LastName)) { LastName = ""; } else { LastName = ERStringManipulation.ERCapitalize(LastName); } if (String.IsNullOrEmpty(CompanyName)) { CompanyName = ""; } else { CompanyName = ERStringManipulation.ERCapitalize(CompanyName); } if (String.IsNullOrEmpty(StreetAddress)) { StreetAddress = ""; } else { StreetAddress = ERStringManipulation.ERCapitalize(StreetAddress); } if (String.IsNullOrEmpty(City)) { City = ""; } else { City = ERStringManipulation.ERCapitalize(City); } //Use your XXStringManipulation.XXExtractDigits to reduce phone to just digits Phone = ERStringManipulation.ERExtractDigits(Phone); if (String.IsNullOrEmpty(Phone) || Phone.Length != 10) { yield return(new ValidationResult("Phone is required", new[] { nameof(Phone) })); } else { string newPhone = Phone.Substring(0, 3) + "-" + Phone.Substring(3, 3) + "-" + Phone.Substring(6, 4); Phone = newPhone; } //At least one of FirstName, LastName or CompanyName must be specified. All can be specified, but is not mandatory. if (FirstName == "" && LastName == "" && CompanyName == "") { yield return(new ValidationResult("Either first name, last name or company is reuired", new[] { nameof(FirstName), nameof(LastName), nameof(CompanyName) })); } //Email Validations if (String.IsNullOrEmpty(Email) && String.IsNullOrEmpty(PostalCode) && String.IsNullOrEmpty(ProvinceCode) && String.IsNullOrEmpty(StreetAddress) && String.IsNullOrEmpty(City)) { yield return(new ValidationResult("Either Email or Postal addressing is required", new[] { nameof(Email), nameof(PostalCode), nameof(ProvinceCode), nameof(StreetAddress), nameof(City) })); } //Province code validation _context = new ERClubsContext(); string foundProvince = ""; string provinceError = ""; if (!String.IsNullOrEmpty(ProvinceCode)) { try { var provinceCode = _context.Province.ToList(); foundProvince = provinceCode.FirstOrDefault(x => x.ProvinceCode == ProvinceCode).ToString(); if (String.IsNullOrEmpty(foundProvince)) { //Provinc code wasnt found provinceError = new ValidationResult("Province was not found", new[] { nameof(ProvinceCode) }).ToString(); } } catch (Exception ex) { //Error fetching province code provinceError = new ValidationResult("Error finding the province", new[] { ex.Message.ToString() }).ToString(); } if (provinceError != "") { yield return(new ValidationResult(provinceError)); } } string postalError = ""; //Postal code validation if (!String.IsNullOrEmpty(PostalCode)) { //If postal provided check for province if (String.IsNullOrEmpty(ProvinceCode)) { yield return(new ValidationResult("Province Code is is required", new[] { nameof(ProvinceCode) })); } //Fetches the country and its postal pattern try { // var foundProvinceCode = _context.Province.ToList(); var foundProvinceCode = _context.Province.FirstOrDefault(x => x.ProvinceCode == ProvinceCode); var countryCode = _context.Country.FirstOrDefault(x => x.CountryCode == foundProvinceCode.CountryCode.ToString()); string postalRegex = countryCode.PostalPattern; //Changes everything to upp case PostalCode = PostalCode.ToUpper(); //Checks if the postal is from canada if (countryCode.CountryCode == "CA") { string firstPostal = foundProvinceCode.FirstPostalLetter; if (!firstPostal.Contains(PostalCode[0])) { postalError = new ValidationResult("First letter of postal doesnt match the province", new[] { nameof(PostalCode) }).ToString(); } else { //If no space was there adds one if (PostalCode.Length == 6) { PostalCode = PostalCode.Insert(3, " "); } } } //Validates the postal pattern if (!ERStringManipulation.ERPostalCodeIsValid(PostalCode, postalRegex)) { postalError = new ValidationResult("Postal Code is not in correct format", new[] { nameof(PostalCode) }).ToString(); } } catch (Exception ex) { postalError = new ValidationResult("Error finding the country", new[] { ex.Message.ToString() }).ToString(); } if (postalError != "") { yield return(new ValidationResult(postalError)); } } yield return(ValidationResult.Success); }