data() { const isValidIp = (rule, value, callback) => { // 校验IP是否符合规则 var regEx = /,/g var ipList = value.toString().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 callback(new Error('请输入节点ip地址,多节点用,分隔')) } else { callback() } } return true } return { form: { name: '', accCode: '', ip: '', nodeIp: '' }, accList: [], rules: { name: [{ required: true, message: '请输入', trigger: 'blur' }], accCode: [{ required: true, message: '请选择', trigger: 'blur' }], ip: [{ required: true, message: '请输入', trigger: 'blur' }], nodeIp: [{ required: true, validator: isValidIp, message: '请输入', trigger: 'blur' }] } } },
import { isValidIp } from '../../../utils/validate'
const validIp = (rule, value, callback) => { // 校验IP是否符合规则 if (!isValidIp(value) { return callback(new Error('请输入节点ip地址,多节点用,分隔')) }else { callback() } }
/** * @param {Array} arg * @returns {Boolean} */ export function isValidIp(arg) { var regEx = /,/g var ipList = arg.toString().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 }else { callback() } } return true }
同时校验IP和IP段
function isValidIp(arg) { var regEx = /,/g var ipList = arg.toString().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])$/ var pattern = /^(\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])\/(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/ for (var i in ipList) { if (!reg.test(ipList[i]) && !pattern.test(ipList[i])) { return false }else { callback() } } return true }