el-input 限制输入框只能输入数字和小数

时间:2025-01-19 17:04:38

1、纯输入框

方法一:

oninput =“value=(/[^\d]/g,‘’)” //只能输入数字

oninput =“value=(/[^0-9.]/g,‘’)” //只能输入数字和小数

<el-input v-model="sjje" oninput ="value=(/[^0-9.]/g,'')" class="money sjje">
	<span slot="suffix">/</span>
</el-input>                           

方法二:

	<el-input v-model="sjje" class="money sjje" @input="inputChange">
      <span slot="suffix">/</span>
    </el-input>

	inputChange(val) {
      this.sjje = this.sjje.replace(/[^0-9.]/g, '')
    }

在这里插入图片描述

2、form表单输入框

<el-form ref="userform" rules="rules" :model="form" label-width="110px">
	<el-row>
		<el-form-item label="年龄:" prop="age">
            <el-input v-model=""></el-input>
        </el-form-item>
	</el-row>
</el-form>

rules:{
  age:[
    { required: true, message: '年龄不能为空', trigger: 'blur'},
    {pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
  ]
},