public static bool AddressInRange(string address, string firstRangeAddress, uint lastOctet) { var validationRule = new AddressValidationRule(); var addressIsValid = validationRule.Validate(address); var rangeStartIsValid = validationRule.Validate(firstRangeAddress); var lastOctetValid = lastOctet > 0 && lastOctet < 255; if (!addressIsValid || !rangeStartIsValid || !lastOctetValid) { return(false); } var addressOctets = address.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var startOctets = firstRangeAddress.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var firstOctetMatch = addressOctets[0] == startOctets[0]; var secondOctetMatch = addressOctets[1] == startOctets[1]; var thirdOctetMatch = addressOctets[2] == startOctets[2]; var finalOctet = uint.Parse(addressOctets[3]); var finalStartRangeOcted = uint.Parse(startOctets[3]); var lastOctedInRange = finalOctet >= finalStartRangeOcted && finalOctet <= lastOctet; return(firstOctetMatch & secondOctetMatch && thirdOctetMatch && lastOctedInRange); }
public static bool AddressInRange(string address, string firstRangeAddress, uint lastOctet) { if (address == null) { throw new ArgumentNullException(nameof(address)); } if (firstRangeAddress == null) { throw new ArgumentNullException(nameof(firstRangeAddress)); } var addressIsValid = AddressValidationRule.Validate(address); var rangeStartIsValid = AddressValidationRule.Validate(firstRangeAddress); var lastOctetValid = lastOctet > 0 && lastOctet < 255; if (!addressIsValid || !rangeStartIsValid || !lastOctetValid) { return(false); } var addressOctets = address.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var startOctets = firstRangeAddress.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var firstOctetMatch = addressOctets[0] == startOctets[0]; var secondOctetMatch = addressOctets[1] == startOctets[1]; var thirdOctetMatch = addressOctets[2] == startOctets[2]; var finalOctet = uint.Parse(addressOctets[3], CultureInfo.CurrentCulture); var finalStartRangeOcted = uint.Parse(startOctets[3], CultureInfo.CurrentCulture); var lastOctedInRange = finalOctet >= finalStartRangeOcted && finalOctet <= lastOctet; return(firstOctetMatch && secondOctetMatch && thirdOctetMatch && lastOctedInRange); }
private void BaseIpTextBox_TextChanged(object sender, System.EventArgs e) { var address = baseIpTextBox.Text; if (_addressValidationRule.Validate(address)) { _viewModel.BaseIp = address; baseIpTextBox.BackColor = DefaultBackColor; } else { baseIpTextBox.BackColor = Color.Red; } }
private void NewAddressTextBoxTextChanged(object sender, EventArgs e) { var input = newAddressTextBox.Text; if (string.IsNullOrWhiteSpace(input)) { addressAddButton.Enabled = false; newAddressTextBox.BackColor = DefaultBackColor; return; } if (_validationRule.Validate(input)) { addressAddButton.Enabled = true; newAddressTextBox.BackColor = Color.LawnGreen; } else { newAddressTextBox.BackColor = Color.Red; addressAddButton.Enabled = false; } }