我在验证 IP 地址时遇到问题。我试图通过将 IP 地址分成 4 个段并分别验证每个段来验证它,尽管我不知道如何将它分解成段。
我已将整个代码放在下面,我将在包含整个代码的代码下方放置另一个代码块,其中仅包含与验证相关的内容
Imports System.Text.RegularExpressions
Public Class Ticket_Request_Form
Dim myXMLFile = "Tickets.xml"
Public strName As String
Public strProblem As String
Public strDateProblemStarted As String
Public strProblemDescription As String
Public strTypeOfProblem As String
Public strIPAddress As String
Public intIPSegment1 As Integer
Public intIPSegment2 As Integer
Public intIPSegment3 As Integer
Public intIPSegment4 As Integer
Public blnAllInputsValid As Boolean = False
Private Sub Ticket_Request_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(myXMLFile) = True Then
DsTickets.ReadXml(myXMLFile)
End If
End Sub
Private Sub TicketsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TicketsBindingNavigatorSaveItem.Click
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to Tickets.xml", "File Save Error")
End Try
strName = txtName.Text
strProblem = txtProblem.Text
strDateProblemStarted = txtDateProblemStarted.Text
strProblemDescription = txtProblemDescription.Text
strIPAddress = txtIPAddress.Text
blnAllInputsValid = False
ValidateUserInputs()
If blnAllInputsValid <> True Then
Exit Sub
End If
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to Tickets.xml", "File Save Error")
End Try
ValidateUserInputs()
If blnAllInputsValid <> True Then
Exit Sub
End If
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to ModLibrary.xml", "File Save Error")
End Try
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
Dim confirm_msg As Integer
confirm_msg = MessageBox.Show("DELETE RECORD FOR " & txtProblem.Text & "?",
"Confirm Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Information)
If confirm_msg = vbYes Then
MessageBox.Show("Record Marked for Deletion." & vbCrLf _
& "Click the Save Icon to Save Changes" & vbCrLf _
& "and Permanently Delete the Record. ", "Delete in Progress")
Else
Me.Controls.Clear()
InitializeComponent()
Ticket_Request_Form_Load(e, e)
MessageBox.Show("Record Delete Cancelled." & vbCrLf _
& "File Returned to First Record.", "Delete Cancelled")
End If
End Sub
Public Sub ValidateUserInputs()
blnAllInputsValid = False
If strName.Length > 0 Then
Else
blnAllInputsValid = False
MessageBox.Show("Please enter your name into the Name Box.", "Input Error")
txtName.Focus()
Exit Sub
End If
If strProblem.Length > 0 Then
Else
blnAllInputsValid = False
MessageBox.Show("Please enter the problem your having into the Problem box.", "Input Error")
txtProblem.Focus()
Exit Sub
End If
If strProblemDescription.Length > 0 Then
Else
blnAllInputsValid = False
MessageBox.Show("Please describe the problem your having into the Problem Description box.", "Input Error")
txtProblem.Focus()
Exit Sub
End If
Dim strUserInputDateProblemStarted As String = txtDateProblemStarted.Text
Dim strInputDateProblemStartedName As String = "Creation Date"
Dim blnDateProblemStartedIsValid As Boolean = False
blnDateProblemStartedIsValid = ValidInputDateProblemStartedCheck(strUserInputDateProblemStarted, strInputDateProblemStartedName)
If blnDateProblemStartedIsValid = False Then
txtDateProblemStarted.Focus()
blnAllInputsValid = False
Exit Sub
End If
Dim CurrentDate As Date = Date.Now
CurrentDate = CurrentDate.ToShortDateString
If strDateProblemStarted > CurrentDate Then
MessageBox.Show("Date of Creation Error: " & strUserInputDateProblemStarted & "." & vbCrLf _
& "Date cannot be in the Future. " & vbCrLf _
& "Current Date Today is: " & CurrentDate & vbCrLf _
& "Re-enter the creation date. ", "Date Error")
txtDateProblemStarted.Focus()
blnAllInputsValid = False
Exit Sub
End If
Dim strUserInputIPAddress As String = txtIPAddress.Text
Dim strInputIPAddressName As String = "IP Address"
Dim blnIPAddressIsValid As Boolean = False
blnIPAddressIsValid = ValidInputIPAddressCheck(strUserInputIPAddress, strInputIPAddressName)
If blnIPAddressIsValid = False Then
txtIPAddress.Focus()
blnAllInputsValid = False
Exit Sub
End If
blnAllInputsValid = True
End Sub
Public Function ValidInputDateProblemStartedCheck(ByVal strIncomingDateProblemStarted As String, ByVal strIncomingName As String) As Boolean
If strIncomingDateProblemStarted = Nothing Then
MessageBox.Show("Please Enter " & strIncomingName, "Data Missing")
ValidInputDateProblemStartedCheck = False
Return ValidInputDateProblemStartedCheck
End If
Dim ValidDateProblemStartedFormat As New Regex("\d{2}-\d{2}-\d{4}")
If ValidDateProblemStartedFormat.IsMatch(strIncomingDateProblemStarted) Then
Else
MessageBox.Show("Invalid Date Format Detected for " & strIncomingName & "." _
& vbCrLf & "The date that the problem started should be 10 characters lond." _
& vbCrLf & "Date should use - as the seperator symbol." _
& vbCrLf & "Required Date Format is dd-mm-yyyy" _
& vbCrLf & "Enter Date like this: 10-07-2006.", "Date Problem Started Format Error")
ValidInputDateProblemStartedCheck = False
Return ValidInputDateProblemStartedCheck
End If
If IsDate(strIncomingDateProblemStarted) Then
Else
MessageBox.Show("Invalid Date Error: " & strIncomingName & "." & vbCrLf _
& "Date is not a Real Date." & vbCrLf _
& "Rejecter Date = " & strIncomingDateProblemStarted, "Date Error")
ValidInputDateProblemStartedCheck = False
Return ValidInputDateProblemStartedCheck
End If
ValidInputDateProblemStartedCheck = True
Return ValidInputDateProblemStartedCheck
End Function
Private Sub btnReviewTicket_Click(sender As Object, e As EventArgs) Handles btnReviewTicket.Click
txtReviewTicket.Text = "Hi " & txtName.Text & vbCrLf &
"Problem: " & txtProblem.Text & vbCrLf &
"Description: " & txtProblemDescription.Text & vbCrLf &
"The problem has been around since: " & txtDateProblemStarted.Text
End Sub
Private Sub imgOpen###########_Click(sender As Object, e As EventArgs) Handles imgOpen###########.Click
Process.Start("https://############################")
End Sub
Public Function ValidInputIPAddressCheck(ByVal strIncomingIPAddress As String, ByVal strIncomingName As String) As Boolean
If strIncomingIPAddress = Nothing Then
MessageBox.Show("Please Enter " & strIncomingName, "Data Missing")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment1 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment1, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment2 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment2, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment3 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment3, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment4 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment4, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
ValidInputIPAddressCheck = True
Return ValidInputIPAddressCheck
End Function
End Class
Imports System.Text.RegularExpressions
Public Class Ticket_Request_Form
Dim myXMLFile = "Tickets.xml"
Public strIPAddress As String
Public intIPSegment1 As Integer
Public intIPSegment2 As Integer
Public intIPSegment3 As Integer
Public intIPSegment4 As Integer
Public blnAllInputsValid As Boolean = False
Private Sub Ticket_Request_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(myXMLFile) = True Then
DsTickets.ReadXml(myXMLFile)
End If
End Sub
Private Sub TicketsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TicketsBindingNavigatorSaveItem.Click
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to Tickets.xml", "File Save Error")
End Try
strIPAddress = txtIPAddress.Text
blnAllInputsValid = False
ValidateUserInputs()
If blnAllInputsValid <> True Then
Exit Sub
End If
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to Tickets.xml", "File Save Error")
End Try
ValidateUserInputs()
If blnAllInputsValid <> True Then
Exit Sub
End If
DtabTicketsBindingSource.EndEdit()
Try
DsTickets.WriteXml(myXMLFile)
MessageBox.Show("Data Saved Successfully", "File Saved")
Catch ex As Exception
MessageBox.Show("Data Not Saved to ModLibrary.xml", "File Save Error")
End Try
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
Dim confirm_msg As Integer
confirm_msg = MessageBox.Show("DELETE RECORD FOR " & txtProblem.Text & "?",
"Confirm Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Information)
If confirm_msg = vbYes Then
MessageBox.Show("Record Marked for Deletion." & vbCrLf _
& "Click the Save Icon to Save Changes" & vbCrLf _
& "and Permanently Delete the Record. ", "Delete in Progress")
Else
Me.Controls.Clear()
InitializeComponent()
Ticket_Request_Form_Load(e, e)
MessageBox.Show("Record Delete Cancelled." & vbCrLf _
& "File Returned to First Record.", "Delete Cancelled")
End If
End Sub
Public Sub ValidateUserInputs()
blnAllInputsValid = False
Dim strUserInputIPAddress As String = txtIPAddress.Text
Dim strInputIPAddressName As String = "IP Address"
Dim blnIPAddressIsValid As Boolean = False
blnIPAddressIsValid = ValidInputIPAddressCheck(strUserInputIPAddress, strInputIPAddressName)
If blnIPAddressIsValid = False Then
txtIPAddress.Focus()
blnAllInputsValid = False
Exit Sub
End If
blnAllInputsValid = True
End Sub
Public Function ValidInputIPAddressCheck(ByVal strIncomingIPAddress As String, ByVal strIncomingName As String) As Boolean
If strIncomingIPAddress = Nothing Then
MessageBox.Show("Please Enter " & strIncomingName, "Data Missing")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment1 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment1, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment2 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment2, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment3 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment3, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
If 0 < intIPSegment4 < 255 Then
Else
MessageBox.Show("Invalid IP Error: " & strIncomingName & "." & vbCrLf _
& "IP is not valid." & vbCrLf _
& "Rejected IP Segment = " & intIPSegment4, "IP Address Error")
ValidInputIPAddressCheck = False
Return ValidInputIPAddressCheck
End If
ValidInputIPAddressCheck = True
Return ValidInputIPAddressCheck
End Function
End Class
如果有人知道我如何将 IP 地址分成变量 intIPSegment1、intIPSegment2、intIPSegment3 和 intIPSegment4,我们将不胜感激。
提前致谢, 轴心
您可以使用 Strings.Split 将您的 IP 地址分解为 4 个八位字节,然后您可以验证:
Dim ipAddress As String = "127.0.0.1"
' Check for "localhost"
If Strings.StrComp(ipAddress, "localhost", CompareMethod.Text) = 0 Then
ipAddress = "127.0.0.1"
End If
If ipAddress.Contains(".") Then
Dim octets As String() = Strings.Split(ipAddress, ".")
If octets.Length = 4 Then
For Each octet As String In octets
' Validate value
Dim value As Integer = CType(octet, Integer)
If value >= 0 And value <= 255 Then
' Valid octet
Else
' Invalid octet
End If
Next
Else
' Incorrect number of octets
End If
Else
' Octets not separated by periods
End If
由于您想指出原始代码中哪个八位字节不正确,您可以使用
For
循环而不是 For Each
循环:
For octet As Integer = 1 To octets.Length
' Validate value
Dim value As Integer = CType(octets(octet - 1), Integer)
If value >= 0 And value <= 255 Then
' Valid octet
Else
' Invalid octet
End If
Next
这样你就可以用
octet
来表示哪个八位字节是无效的。