I am working on a PowerShell GUI. For one of the fields I want to lock it down to only numbers and periods, for IP address input. After several attempts at regex I ended up cobbling this together:
$IPInput = New-Object System.Windows.Forms.TextBox $IPInput.Location = New-Object System.Drawing.Point(445, 60) $IPInput.Size = New-Object System.Drawing.Size(170, 20) $IPInput.TextAlign = 'Center' $IPInput.MaxLength = 15 $IPInput.add_TextChanged({ if($IPInput.Text -match '[A-Z!@#$%^&*();;_=+<,>?/|\`"`~{[}}-]') { $IPInput.Text = $IPInput.Text -replace '[A-Z!@#$%^&*();;_=+<,>?/|\`"`~{[}}-]' if($IPInput.Text.Length -gt 0) { $IPInput.Focus() $IPInput.SelectionStart = $IPInput.Text.Length } } })
This works for every character except a single quote ('). I have tried putting a ` before the quote. Changing the enclosing quotes to “” and then adding the single quote character, and about every other permutation I could think of. Can anyone suggest the best method for including the single quote, or an easy way to exclude anything and everything except numbers and periods?