private void AddressList_Validating(object sender, System.ComponentModel.CancelEventArgs e) { bool isValid = false; // Assume that the address is invalid until proven otherwise TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text}"); // Test whether the supplied IP address is valid and, if it is an IPv6 address, test whether it is in canonical form if (IPAddress.TryParse(addressList.Text.Trim(), out IPAddress ipAddress)) // The host name is an IP address { if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) // This is an IPv6 address { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is an IPv6 address"); if (addressList.Text.Trim().StartsWith("[") & addressList.Text.Trim().EndsWith("]")) { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is a canonical IPv6 address"); // The IP v6 address is already in canonical form, no action required isValid = true; } else // The IPv6 address is not in canonical form so we need to add square brackets { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a canonical IPv6 address"); SetupErrorProvider.SetError(addressList, "IPv6 addresses must be in canonical form i.e. start with [ and end with ]."); } } else // This is an IPv4 address { // The IP v4 address is already in canonical form, no action required isValid = true; } } else // The host name is either an invalid IP address or a string so validate this { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a valid IP address"); MatchCollection matches = validHostnameRegex.Matches(addressList.Text); if (matches.Count == 0) { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is NOT a valid IP address or Host Name"); SetupErrorProvider.SetError(addressList, "Not a valid IP address or host name."); } else { TL.LogMessage("AddressList_Validating", $"Address item: {addressList.Text} is a valid Host Name"); isValid = true; } } if (isValid) { SetupErrorProvider.Clear(); btnOK.Enabled = true; } else { btnOK.Enabled = false; } }
private void AddressList_Validating(object sender, System.ComponentModel.CancelEventArgs e) { bool isValid = false; if (IsIpAddress(addressList.Text)) // The host name is an IP address so test whether this is valid { MatchCollection matches = validIpAddressRegex.Matches(addressList.Text); if (matches.Count == 0) { SetupErrorProvider.SetError(addressList, "IP addresses can only contain digits and the point character in the form WWW.XXX.YYY.ZZZ."); } else { isValid = true; } } else // The host name is a string rather than an IP address so validate this { MatchCollection matches = validHostnameRegex.Matches(addressList.Text); if (matches.Count == 0) { SetupErrorProvider.SetError(addressList, "Not a valid host name."); } else { isValid = true; } } if (isValid) { SetupErrorProvider.Clear(); btnOK.Enabled = true; } else { btnOK.Enabled = false; } }