Mask for IP address

How do I allow only an IP address in a TextField? Can I do this with a Mask or do I need to do a simple Regex?

i think in designer its named validation mask but ###\.###\.###\.### would allow 999.999.999.999
regular expressions would be better.

And of course, one should think about IPV4 vs. IPV6 support.

@Markus Rauch: thanks, that’s better than nothing. Then I’ll tackle a Regex.

@Michael Diehr: in a local network, too?

Nice new site for regex design/testing: https://ihateregex.io/

I find the diagrams very helpful, and the regexes work well in Kem’s tool.

i don’t understand this xojo Regex classes … at least not intuitive

Was there something you struggle with?

yes i just want that any input change into a ip4 number like this.

Sub LostFocus() Handles LostFocus 'IP4 Var columns(-1) As String = Me.Value.Split(".") While columns.Count<4 columns.AddRow("0") Wend For c As Integer = 0 To 3 Var value As Integer = columns(c).ToInteger If value< 0 Then value =0 If value > 255 Then value =255 columns(c) = value.ToString("0") Next Me.Value = columns(0) + "." + columns(1) + "." + columns(2) + "." + columns(3) End Sub
in the TextField TextChanged event with regex.
i think it is not possible direct at input.

at lost focus is the wrong place.

If your code works then you don’t need a Regex. I’m doing the check before the data is saved:

Private Function CheckIP(theText as String) as Boolean dim theRegex as new RegEx theRegex.options.greedy = False theRegex.searchPattern = "(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]?)){3}" dim theRegexMatch as RegExMatch = theRegex.search(theText) return (theRegexMatch <> nil) End Function

Or from https://forum.xojo.com/12416-test-if-a-given-string-is-an-ip-a-address/0#p92694

[code]Dim rg as New RegEx
Dim match as RegExMatch

rg.SearchPattern = “(?:(?: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]?)$”
match = rg.Search(s)

Return (match <> Nil)[/code]

For Web:
AddressField (Super: WebTextField)

Sub LostFocus() Handles LostFocus
CheckIPAdress(me, " " + langIPAddress)
End Sub

Public Sub CheckIPAdress(MyTextField as WebTextField, InfoString as string)
if MyTextField.Text.IsEmpty then exit sub
if MyTextField.Text = “0.0.0.0” then
MsgBox"Not a valid IP " + chr(34) + me.Text + chr(34)
MyTextField.Style = StyleWrongIP
MyTextField.SetFocus
end if

Dim rg As New RegEx
Dim myMatch As RegExMatch
rg.SearchPattern = “^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$”
myMatch = rg.Search(MyTextField.Text)
If myMatch = Nil Then
MsgBox"Not a valid IP " + chr(34) + me.Text + chr(34)
MyTextField.Style = StyleWrongIP
MyTextField.SetFocus
else
MyTextField.Style = StyleNormal
End If

Exception err As RegExException
MsgBox(err.Message)
End Sub

Usage:
Add AddressField to a WebPage.

For Windows, macOS, Linux
AddressField (Super: TextField)

Function KeyDown(Key As String) Handles KeyDown as Boolean
CheckKey(me, Key)
End Function

Sub KeyUp(Key As String) Handles KeyUp
CheckIPAdress(me)
End Sub

Sub LostFocus() Handles LostFocus
CheckIPAdress(me)
End Sub

Public Sub CheckIPAdress(MyTextField as TextField)
if MyTextField.value.len = 0 then exit sub
if MyTextField.value = “0.0.0.0” then
MessageBox “Not a valid IP”
MyTextField.SetFocus
end if

Dim rg As New RegEx
Dim myMatch As RegExMatch
rg.SearchPattern = “^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$”
myMatch = rg.Search(MyTextField.value)
If myMatch = Nil Then
beep
MessageBox ErrorMessage
IPError = true
MyTextField.Bold = TextBold
MyTextField.TextColor = ErrorColor
MyTextField.TextColor = &cFFFFFF00
MyTextField.BackColor = ErrorColor
MyTextField.SetFocus
else
MyTextField.Bold = false
MyTextField.TextColor = DefaultColor
MyTextField.BackColor = &cFFFFFF00
IPError = false
End If

Exception err As RegExException
MessageBox(err.Message)
End Sub

Public Sub CheckKey(MyTextField as TextField, MyKey as string)
if instr(“0123456789.”,MyKey) > 0 or MyKey.asc < 32 or MyKey.asc = 127 then
if MyKey = “.” then
dim MyIPArray(-1) as string
MyIPArray = split(MyTextField.text,".")
If ubound(MyIPArray) > 3 then
MessageBox(“Only 3 Points”)
dim p as PresskeyMBS
p = new PresskeyMBS
p.Charcode = 8
p.virtualCode = -1
p.Press
end if
end if
else
MessageBox(“Only 3 Points”)
dim p as PresskeyMBS
p = new PresskeyMBS
p.Charcode = 8
p.virtualCode = -1
p.Press
end if
End Sub

Usage:
Add AddressField to a Window.