protected void btnValid_Click(object sender, EventArgs e) { OrderNumber = txtOrderNo.Text.Trim(); OrderType = dlistOrderType.Text.Trim(); ClearData(); if (isDataValid()) { //Get Tax Rate if (TaxCode == "") { dlistTax.SelectedItem.Text = "0"; } else { dlistTax.SelectedValue = TaxCode; } switch (Branch.ToUpper()) { case "B40": if (TaxCode.ToUpper() != "") { //lblValidData.Visible = true; lblValidData.Text = "B40 Order with Tax - Error!"; btnPrint.Enabled = false; } btnPrint.Enabled = true; break; case "B50": if (txtTaxCode.Text != "") { txtExRate.Enabled = true; RegularExpressionValidator3.Enabled = true; RequiredFieldValidator1.Enabled = true; txtExRate.Focus(); btnPrint.Enabled = false; } else { txtExRate.Enabled = false; RegularExpressionValidator3.Enabled = false; RequiredFieldValidator1.Enabled = false; btnPrint.Enabled = true; } break; case "B120": //if (TaxCode == "") dlistTax.SelectedItem.Text = "0"; //else dlistTax.SelectedValue = TaxCode; btnPrint.Enabled = true; break; default: //lblValidData.Visible = true; //lblValidData.Text = "Just can print for B40, B50 and B120"; btnPrint.Enabled = true; break; } getToList(); } else { btnPrint.Enabled = false; lblPartner.Visible = false; } }
//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) { //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); }
// Edit Method public void BPEdit() { Regex pattern = new Regex("^[A-Z][A-Z]$"); Regex patternTwo = new Regex("^[A-Z][A-Z]+$"); ProvinceCode = (ProvinceCode + "").Trim(); // trims access ProvinceCode = ProvinceCode.ToUpper(); // makes all letters Upper Case Name = (Name + "").Trim(); // trims access Name = char.ToUpper(Name[0]) + Name.Substring(1); CountryCode = (CountryCode + "").Trim(); CountryCode = CountryCode.ToUpper(); // makes all letters Upper Case TaxCode = (TaxCode + "").Trim(); // trims access TaxCode = TaxCode.ToUpper(); // makes all letters Upper Case string errors = ""; if (Name == "") // if name is left blank error message will display { errors += "Name cannot be empty\n"; // error message } if (!pattern.IsMatch(ProvinceCode)) // ensures that it has 2 characters { errors += "Province code does not match. Must be two letters\n"; } if (!pattern.IsMatch(CountryCode)) // ensures that it has 2 characters { errors += "Country code does not match. Must be two letters\n"; // error message } foreach (BPProvince province in BPGetProvinces()) { if (province.ProvinceCode == ProvinceCode) { isEdit = true; } else if (province.Name == Name) // doesnt allow user to enter same province twice { errors += "Name is already in use by other province code\n"; } } if (TaxCode != "") { if (!patternTwo.IsMatch(TaxCode)) { errors += "Must be empty or contain two numbers only \n"; } else if (TaxRate < 0 || TaxRate > 1) // ensures that tax rate isnt greater than 1 { errors += "Tax rate must be between 0 and 1 inclusive \n"; } } else { if (TaxRate != 0) { errors += "If Tax Rate field is empty must be 0\n"; } } if (TaxRate == 0 && IncludesFederalTax) { errors += "If Tax Rate is 0, must not include Tax Rate\n"; } if (errors != "") { throw new Exception(errors); } }