IP正则表达式: /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/

vue form表单,代码如下:

<template>
    <div>
        <el-form v-model="form" :rules="rules" ref="form">
           <el-form-item label="IP地址" prop="ip_address">
                <el-input v-model.trim="form.ip_address" placeholder="ip地址"></el-input>
          </el-form-item>
            <el-form-item>
            <el-button type="primary" @click="saveForm('form')">保存</el-button>
            <el-button @click="cancelCreate('form')">取消</el-button>
          </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        data() {
           let validateIPAddress = (rule, value, callback) => {
               let regexp = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/;
               let valdata = value.split(',');
               let isCorrect = true;
               if (valdata.length) {
                   for (let i = 0; i < valdata.length; i++) {
                        if (regexp.test(valdata[i]) == false) {
                            isCorrect = false;
                        }
                   }
               }

               if (value == '') {
                    return callback(new Error('请输入iP地址'));
               } else if (!isCorrect) {
                    callback(new Error('请输入正确对ip地址'));
               } else {
                    callback()
               }
            }
            return {
                form: {
                    ip_address: ''
                },
                rules: {
                    ip_address: [{
                        required: true, validator: validateIPAddress, trigger: ['blur']
                    }]
                }
            }
        },
        methods: {
            // 表单提交
            saveForm(formName) {
                  this.$refs[formName].validate((valid) => {
                    if (valid) {
                          // 调用api
                    }
                  })
            },
            // 取消表单
            cancelCreate(formName) {
                this.cancelForm(formName);
            },
             // 重置表单
            cancelForm(formName) {
                this.$refs[formName].resetFields();
            },
        }
    }
</script>

 

Logo

前往低代码交流专区

更多推荐