javascript验证Ip高手【请进】

JavaScript026

javascript验证Ip高手【请进】,第1张

<script>

function ValidateIp()

{

var ip = document.getElementById('ip').value

var patrn = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):[1-9][0-9]*$/

var ipArray = ip.split("")

var invalidIpIndex = 0

for(var ipIndex in ipArray){

var anIp = ipArray[ipIndex]

invalidIpIndex++

if(!patrn.exec(anIp)){

window.alert('第'+invalidIpIndex+'个地址不正确!')

return false

}

var ipPort = anIp.split(":")

if(!ipPort[1] || ipPort[1] >65535){

window.alert('第'+invalidIpIndex+'个地址端口不正确!')

return false

}

}

window.alert('地址正确')

}

</script>

<input id="ip"></input>

<input type="button" value="validate" onClick="ValidateIp()">

//检测多个ip地址用逗号分隔

function checkIpv6(ip) {

var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/

var valdata = ip.split(',')

for(i=0i<valdata.lengthi++){

if(reg.test(valdata[i])== false){

layer.alert("Ip格式不正确")

return false

}

}