节点IP地址校验ip规则,思路:取出输入文本,通过","分割获取list,遍历list,校验list里每个对象是否符合ip输入规则。
function isValidIp(ipStr) { var regEx = /,/g; var ipList = ipStr.replace(regEx, ',').split(','); 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])$/; for (var i in ipList) { if (!reg.test(ipList[i])) { return false } } return true }
var ipTest = '127.0.0.1,127.0.0.5' //true var ipTest1 = '127.0.0.1,127.0.0.256' //false var ipTest2 = '127.0.0.1 127.0.0.5' //false var ipTest3 = '127.0.0.1,127.0.5' // false var ipTest4 = '127.0.0.256,127.0.0.5' //false var ipTest5 = '127.0.0.256,,127.0.0.5' //false var ipTest6 = '127.0.0.252,,127.0.0.5' //false