I have a textbox C# for IP addressing; validating IP address. However, I'm trying to limit to numbers and the amount of dots a user can enter in for the IP address. This way it limits errors.
Seems I'm able to enter one dot, I would like to increase that number to three. I can create a "Regex.IsMatch" and validate using "IPAddress", but I'm just trying to limit what the user can enter before pressing a button to proceed.
Is there a way to do this? Searching the Internet haven't found any way to do this.
string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
bool CKDots = Regex.IsMatch(TracertIP, pattern);
private void txtTracerouteIP_KeyPress(object sender, KeyPressEventArgs e)
{
// Enter only numbers.
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// Only one dot, but I need three.
//if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
//{
// e.Handled = true;
//}
}