【ASP】判断是否是合法的IP地址源代码
				
									
					
					
						|  | 
							admin 2024年11月12日 18:35
								本文热度 2058 | 
					
				 
				【ASP】判断是否是合法的IP地址源代码
<%
Function IsValidIP(ipAddress)
    Dim regexObj, matches
    Set regexObj = New RegExp
    regexObj.Pattern = "^(?:(?: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]?)$"
    regexObj.IgnoreCase = True
    Set matches = regexObj.Execute(ipAddress)
    IsValidIP = matches.Count > 0
    Set regexObj = Nothing
End Function
 
' 测试IP地址
Dim testIP
testIP = "192.168.1.1"
 
If IsValidIP(testIP) Then
    Response.Write("IP 地址 " & testIP & " 是有效的。")
Else
    Response.Write("IP 地址 " & testIP & " 无效。")
End If
%>
这段代码定义了一个名为IsValidIP的函数,它接受一个字符串作为参数,并使用正则表达式来检查这个字符串是否符合IP地址的格式。如果匹配,则返回True,否则返回False。然后,代码使用了一个测试IP地址来测试这个函数。
该文章在 2024/11/12 18:35:46 编辑过