在vue2.0中messagebox中实现input、el-input-number输入及正则表达验证功能,
时间: 2024-03-10 17:45:05 浏览: 62
在Vue 2.0中,可以通过使用Element UI库中的MessageBox组件和Input、InputNumber组件来实现输入和正则表达式验证功能。
以下是一个简单的示例代码:
<template>
<div>
<el-button @click="showInputBox">显示输入框</el-button>
<el-button @click="showNumberBox">显示数字输入框</el-button>
</div>
</template>
<script>
export default {
methods: {
showInputBox() {
this.$prompt('请输入内容', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputPattern: /^[a-zA-Z0-9]+$/,
inputErrorMessage: '只能输入字母或数字'
}).then(({ value }) => {
console.log(value)
}).catch(() => {})
},
showNumberBox() {
this.$prompt('请输入数字', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputType: 'number',
inputPattern: /^[0-9]+$/,
inputErrorMessage: '只能输入数字'
}).then(({ value }) => {
console.log(value)
}).catch(() => {})
}
}
}
</script>
在上面的代码中,我们使用了this.$prompt
方法来显示输入框,其中通过inputType
属性指定了输入框类型,通过inputPattern
属性指定了正则表达式验证规则,通过inputErrorMessage
属性指定了验证错误提示信息。
在实际使用中,你可以根据自己的需要修改上述代码,实现自己的输入和验证功能。
相关推荐









