IP textfield IPV4

  1. Create a textfield control
  2. Change Mask property to #99.#99.#99.#99
  3. 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
//replace the number and the .
me.text = Replace(me.text, s(i) + “.”, “”)
Exit For i
end if


//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

end if

Next i[/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 = 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
    end if
Next i

perhaps this might do the same and be a bit cleaner??? (OFF THE TOP OF MY HEAD)


After changing s(i)=str(val(ss)) to s(i)=str(ss) (ss is already an integer)…

Your code, while shorter, doesn’t function correctly.

ok…seems to do what I could tell based on yours… but no worries

How are we supposed to have a Sunday morning code war if you give up that easy?

Because I am not here to “fight”… I offered what I thought was a cleaner solution… and obviously I misinterpeted your original code…

provides no insight…

My goodness, I don’t even…

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)

It was 8:40 on a Sunday morning for Dave, so he might not have had a coffee yet … :wink:

Btw are you sure about your mask property? It doesn’t cover for example …

@Markus Winter hmm I tried that ip and it seems to allow it.

The mask # property means it HAS to be a number
The mask 9 property means it CAN be a number (but can be blank)

so single digits would be valid?

@Dave S

  1. This doesn’t place the decimal points in for you
  2. if a user types 001 your code goes bonkers.

Sorry your having a bad morning…hope your day gets better!


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.

These make it MORE annoying to use … not less.


Is this forum moderated? Can I get an adult?

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
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
If t="" Or Right(t,1)="." Then Return False // 1st char a #
If Val(s)>255 Then Return True // each segment cannot exceed 255
If Right(t,1)=“0” Then
Return False
End If

// ADD THESE 4 LINES to resolve issue #1
If Len(s)=3 And v.Ubound<3 Then
Return True
End If

End If[/code]

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.