Resolving input to get IP Address

kasdoffe

Well-known member
Joined
Aug 27, 2003
Messages
57
Ill make this simple. I have a form w/ a text box where a user can enter anything.

This text box is suppose to be used for a server name or IP address.

I am creating a IPHostEntry using the System.Net.Dns.GetHostByName() method.

I have a try/catch statement around the above call and
it seems to try/catch fine if a user enters a real looking name or a real looking IP Address. It will continue on if correct or catch the error if the machine or IP doesnt exist.

However, if i just type in junk numbers like, "3245762345762345073045", it seems to create an IPHostEntry without any problem.

Is there a way to verify that an entered text is a valid Host or IP address?

I always want the IP address from whatever they input...
 
Use the masked edit control:

Visual Basic: Masked Edit Control

Masked Edit ControlSee Also
Properties, Methods, and Events | Masked Edit Control Constants | ValidationError Event
The Masked Edit control provides restricted data input as well as formatted data output. This control supplies visual cues about the type of data being entered or displayed. This is what the control looks like as an icon in the Toolbox:

File Name
MSMASK32.OCX

Class Name
MaskEdBox

Remarks
The Masked Edit control generally behaves as a standard text box control with enhancements for optional masked input and formatted output. If you do not use an input mask, the Masked Edit control behaves much like a standard text box.

If you define an input mask using the Mask property, each character position in the Masked Edit control maps to either a placeholder of a specified type or a literal character. Literal characters, or literals, can give visual cues about the type of data being used. For example, the parentheses surrounding the area code of a telephone number are literals: (206).

If you attempt to enter a character that conflicts with the input mask, the control generates a ValidationError event. The input mask prevents you from entering invalid characters into the control.

The Masked Edit control has three bound properties: DataChanged, DataField, and DataSource. This means that it can be linked to a data control and display field values for the current record in the recordset. The Masked Edit control can also write out values to the recordset.

When the value of the field referenced by the DataField property is read, it is converted to a Text property string, if possible. If the recordset is updatable, the string is converted to the data type of the field.

To clear the Text property when you have a mask defined, you first need to set the Mask property to an empty string, and then the Text property to an empty string:

MaskedEdit1.Mask = ""
MaskedEdit1.Text = ""
When you define an input mask, the Masked Edit control behaves differently from the standard text box. The insertion point automatically skips over literals as you enter data or move the insertion point.

When you insert or delete a character, all nonliteral characters to the right of the insertion point are shifted, as necessary. If shifting these characters leads to a validation error, the insertion or deletion is prevented, and a ValidationError event is triggered.

Suppose the Mask property is defined as "?###", and the current value of the Text property is "A12." If you attempt to insert the letter "B" to the left of the letter "A," the "A" would shift to the right. Since the second value of the input mask requires a number, the letter "A" would cause the control to generate a ValidationError event.

The Masked Edit control also validates the values of the Text property at run time. If you set the Text property so that it conflicts with the input mask, the control generates a run-time error.

You may select text in the same way as for a standard text box control. When selected text is deleted, the control attempts to shift the remaining characters to the left of the selection.

Normally, when a selection in the Masked Edit control is copied onto the Clipboard, the entire selection, including literals, is transferred onto the Clipboard. You can use the ClipMode property to transfer only user-entered data onto the Clipboard
 
Originally posted by kasdoffe
Ill make this simple. I have a form w/ a text box where a user can enter anything.

This text box is suppose to be used for a server name or IP address.

I am creating a IPHostEntry using the System.Net.Dns.GetHostByName() method.

I have a try/catch statement around the above call and
it seems to try/catch fine if a user enters a real looking name or a real looking IP Address. It will continue on if correct or catch the error if the machine or IP doesnt exist.

However, if i just type in junk numbers like, "3245762345762345073045", it seems to create an IPHostEntry without any problem.

Is there a way to verify that an entered text is a valid Host or IP address?

I always want the IP address from whatever they input...

Code:
static void Main(string[] args)
{
	//wont throw error
	IPHostEntry iphe = Dns.GetHostByName("www.cnn.com");
	if(iphe.AddressList.Length > 0)
	{
		IPAddress address = iphe.AddressList[0];
		iphe = Dns.GetHostByAddress(address);
	}
	//will throw error
	iphe = Dns.GetHostByName("3245762345762345073045");
	if(iphe.AddressList.Length > 0)
	{
		IPAddress address = iphe.AddressList[0];
		iphe = Dns.GetHostByAddress(address);
	}
	return;
}
 
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ipEntry As Net.IPHostEntry = Net.Dns.Resolve(Net.Dns.GetHostName)
        Dim ip As Net.IPAddress
        Dim result As String
        For Each ip In ipEntry.AddressList()
            If ip.ToString = TextBox1.Text Then
                result = ("the ip: " & ip.ToString & " exists on this machine!")
            Else
                result = ("you entered an invalid IP!")
            End If
        Next
        MessageBox.Show(result)
    End Sub
 
Back
Top