Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// validate all inputs correct and reformat if it is not right format
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (YKNumericUtilities.IsNumeric(txtNumeric.Text))
            {
                lblNumericResult.Text = "Inputed String is Numeric";
            }
            else
            {
                lblNumericResult.Text = "Inputed String is not Numeric";
            }

            if (YKNumericUtilities.IsInteger(txtInteger.Text))
            {
                lblIntegerResult.Text = "Inputed String is Integer";
            }
            else
            {
                lblIntegerResult.Text = "Inputed String is not Integer";
            }

            string result = "";

            result = YKNumericUtilities.MakeNumber(txtMakeNumbers.Text);
            if (result == null)
            {
                lblMakeNumbersResult.Text = "Making Numbers is failed";
            }
            else
            {
                lblMakeNumbersResult.Text = result.ToString();
            }

            lblNameResult.Text = YKStringUtilities.Capitalize(txtName.Text);

            lblAddressResult.Text = YKStringUtilities.Capitalize(txtAddress.Text);

            lblCityResult.Text = YKStringUtilities.Capitalize(txtCity.Text);

            if (YKValidations.ValidatePhoneNumber(txtPhoneNumber.Text))
            {
                lblPhoneNumberValid.Text = "Phone number is valid";

                lblPhoneNumberResult.Text = YKStringUtilities.FormatPhoneNumber(txtPhoneNumber.Text);
            }
            else
            {
                lblPhoneNumberValid.Text = "Phone number is not valid";
            }


            if (YKValidations.ValidateCanadianPostalCode(txtPostalCode.Text))
            {
                lblPostalCodeValid.Text = "Postcal Code is valid";

                lblPostalCodeResult.Text = YKStringUtilities.FormatCanadianPostalCode(txtPostalCode.Text);
            }
            else
            {
                lblPostalCodeValid.Text = "Postcal Code is not valid";
            }

            if (YKValidations.ValidateUPZipCode(txtUSPostalCode.Text))
            {
                lblUSPostalCodeValid.Text = "US Postcal Code is valid";

                lblUSPostalCodeResult.Text = YKStringUtilities.FormatUSZipCode(txtUSPostalCode.Text);
            }
            else
            {
                lblUSPostalCodeValid.Text = "US Postcal Code is not valid";
            }

            lblFullNameResult.Text = YKStringUtilities.MakeFullName(txtFirstName.Text, txtLastName.Text);
        }
Exemplo n.º 3
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string errorMessage = "";

            /*
             * YKFullName(string1, string2) return string
             *  combine firstname and lastname then makes it to full name format
             */
            txtFullName.Text = YKStringUtilities.YKFullName
                                   (txtFirstName.Text, txtLastName.Text);
            if (txtFullName.Text == "")
            {
                errorMessage += "Please input FirstName, LastName or Both\n";
            }

            /*
             * IsInteger(string) return bool
             *  Check the string is Integer or not, by regex
             *
             * IsInteger(int) return bool
             *  Check the string is Integer or not, by regex
             */

            if (rbtInt.Checked)
            {
                int age;
                if (int.TryParse(txtAge.Text, out age))
                {
                    if (!YKNumericUtilities.IsInteger(age))
                    {
                        errorMessage += "Age is Invalid\n";
                    }
                }
                else if (!YKNumericUtilities.IsInteger(txtAge.Text))
                {
                    errorMessage += "Age is Invalid\n";
                }
            }
            else if (rbtString.Checked)
            {
                if (!YKNumericUtilities.IsInteger(txtAge.Text))
                {
                    errorMessage += "Age is Invalid\n";
                }
            }
            else
            {
                errorMessage += "Choose the radio button for Age DataType\n";
            }


            /*
             *  YKCanadaPostal(string) return bool
             *      Check the string is CanadaPostal Code format or not, by regex
             *
             *  YKFormatPostal(string) return string
             *      Reformat string to CanadaPostal Code format.
             */
            if (!YKValidations.YKCanadaPostal(txtCanadaPostal.Text))
            {
                errorMessage += "Canada Postal Code is Invalid\n";
            }
            else
            {
                lblCanadaPostal_result.Text = YKStringUtilities.YKFormatPostal
                                                  (txtCanadaPostal.Text);
            }


            /*YKUSPostal(string) return bool
             *  Extract digits by YKOnlyDigits then chech the length is 5 or 9
             *
             * YKOnlyDigits(string) return string
             *  Extract digits from string*/
            if (!YKValidations.YKUSPostal(txtUSPostal.Text))
            {
                errorMessage += "US Postal Code is Invalid\n";
            }
            else
            {
                lblUSPostal_result.Text = YKStringUtilities.YKOnlyDigits
                                              (txtUSPostal.Text);
                lblUSPostal_result.Text = YKStringUtilities.YKReFormat
                                              (lblUSPostal_result.Text);
            }


            /*YKPhoneNumber(string) return bool
             *  Extract digits by YKOnlyDigits then chech the length is 10 or not
             *
             * YKOnlyDigits(string) return string
             *  Extract digits from string
             *
             * YKReFormat(string) return string
             *  if the string is length of 10, reformat it to PhoneNumber format*/
            if (!YKValidations.YKPhoneNumber(txtPhoneNumber.Text))
            {
                errorMessage += "Phone Number is Invalid\n";
            }
            else
            {
                lblPhoneNumber_result.Text = YKStringUtilities.YKOnlyDigits
                                                 (txtPhoneNumber.Text);
                lblPhoneNumber_result.Text = YKStringUtilities.YKReFormat
                                                 (txtPhoneNumber.Text);
            }


            /*Print ERROR MESSAGE when the errorMessage is not empty*/
            if (errorMessage == "")
            {
                lblError.Text = "Successfully Submitted!\n";
            }
            else
            {
                lblError.Text = errorMessage;
            }
        }