Reputation: 25
I'm trying to create a text box for IP address input (eg. 000.000.000.000) via a Windows Powershell gui using Sapien Powershell Studio.
I looked into Maskedtextbox but it doesn't display so well (the _) and I can't limit the range (up to 255) on input.
I've also looked into NumericUpDown which would be perfect if I could hide the arrows as it gives me the ability to limit the range and sets to numbers only.
If not possible to restict on input, I'm thinking of just having 4 text boxes with a MaxLength=3 and a "TextChanged" event validation to wipe text if it not numeric or within range.
Am I overlooking an easier/better way?
Upvotes: 1
Views: 3500
Reputation: 72630
Personaly I agree with @Kev, but I use a regular expression for that.
$regip=[regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
$regip.IsMatch("10.1.1.10")
Upvotes: 1
Reputation: 119806
Don't try to be too smart with your text box, allow your users to enter the IP address without having to fiddle with up/down arrows etc, let them input in a free-form way. They'll thank you for this.
Then upon submission of your form or perhaps exiting the field try to parse the IP address by doing something like:
$ip_address = $null
$a = [System.Net.IPAddress]::TryParse($ip_address_to_validate, [ref]$ip_address)
if ($ip_address -ne $null)
{
# It's valid
}
else
{
# Not valid - display an error message on form
}
Upvotes: 2