Enter the following code in the texfield TextChange event
[code]Dim i as Integer
Dim s(-1) as String
dim ss as integer
//Split the IP text into individual parts
s = Split(me.text, “.”)
//loop through and check each value
For i = 0 to Ubound(s)
//cast to int to remove leading zeros
ss = val(s(i))
//Is the value greater than 255?
if ss > 255 then
//Is this the last number in the IP?
if i = 3 then
//replace the last number without the .
me.text = Replace(me.text, s(i), “”)
Exit For i
else
//replace the number and the .
me.text = Replace(me.text, s(i) + “.”, “”)
Exit For i
end if
else
//remove any leading zeros, if applicable
if Len(s(i)) = 3 AND ss < 100 then
me.text = Replace(me.text, s(i), str(ss))
Exit For i
end if
Dim i as Integer
Dim s(-1) as String
dim ss as integer
//Split the IP text into individual parts
s = Split(me.text, ".")
//loop through and check each value
For i = Ubound(s) downto 0
//cast to int to remove leading zeros
ss = val(s(i))
//Is the value greater than 255?
if ss > 255 then
s.remove i
continue
else
s(i)=str(val(ss))
end if
Next i
me.text=join(s,".")
perhaps this might do the same and be a bit cleaner??? (OFF THE TOP OF MY HEAD)
Perhaps you misunderstood the reason for my post. I wasn’t seeking assistance, I was searching for a way to create a textfield that forces the user to enter a IP address in the correct manner. I was unable to find such a solution, so I wrote one… and then posted it here for others to use and improve on.
You may have also misunderstood my second post to you, which was in jest (hence the smiley face)
Please stop adding “better” solutions to this post that are untested and not functioning.
The whole idea here is to make it easy to type in an ip and have it formatted properly.
No it does not work fine… typing 001 moves the users cursor to the beginning of the text box and adds a zero. 001 is a perfectly valid number. 001 is 1 … 000 is 0 … 045 is 45
typing any 3 digit number also moves the cursor to the beginning of the text box.
meaning you have to click the textbox and move the cursor every time you enter a number.
Dave the code that you posted did not function properly, and since you deleted it I will post it for you again. I have the code because I tested it… and it was not functioning in the same way, or even better then the code I put up. As I said, your code keeps moving the cursor to the beginning of the text box, no matter if the number was “valid” or not. This, in my mind, makes the code worse… not better.
I would love to see any solution that is better, that you can come up with… but continually posting untested and un-working code does not help anyone and only serves to fill this post up with nonsense.
Although judging from the childish PM you sent me, I would imagine I won’t have the pleasure of interacting with you again.
[code]/// IPV4 = ###.###.###.###
// First off only “0-9”, Bkspace, Tab, Enter and Return are accepted
Dim s As String
Dim v() As String
Dim t As String=Me.Text
If AscB(key)<&H20 Then Return False
If Not IsNumeric(key) And key<>"." Then Return True
v=Split(t,".")
If key="." Then
If t="" Or Right(t,1)="." Or v.ubound=3 Then Return True // first char cannot be “.”, and no “…”
Return False // acceptable
Else
If t="" Or Right(t,1)="." Then Return False // 1st char a #
s=v(v.ubound)+key
If Val(s)>255 Then Return True // each segment cannot exceed 255
If Right(t,1)=“0” Then
Me.Text=Left(t,Len(t)-1)
Return False
End If
// ADD THESE 4 LINES to resolve issue #1
If Len(s)=3 And v.Ubound<3 Then
Me.Text=t+key+"."
Return True
End If
Thanks for your post, Jonathan. Dave, thanks for your suggestion. I’m locking this thread because it doesn’t look like it’s going to continue being productive.