supriya nikam

supriya nikam

  • 1.6k
  • 86
  • 4.3k

Text box only allow IP address in windows application

Dec 30 2020 4:49 AM
private void txtSta2_IP_Adrs_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
// Display a pop-up help topic to assist the user.
Help.ShowPopup(txtSta2_IP_Adrs, "enter ip", new Point(txtSta2_IP_Adrs.Right, this.txtSta2_IP_Adrs.Bottom));
}
bool isIPAddress = false;
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";
Regex regex = new Regex(pattern);
isIPAddress = regex.IsMatch(pattern);
//string ip1 = "192.168.0.1";
// string ip2 = "302.0.0.1";
//Regex.IsMatch(ip1, pattern); // returns true
// Regex.IsMatch(ip2, pattern); // returns false
return isIPAddress ;
}

Attachment: Untitled1.rar

Answers (1)