element ui 表单校验pattern
时间: 2024-06-06 07:04:49 浏览: 19
Element UI 是一套基于 Vue.js 2.0 的桌面端组件库,它提供了一系列的表单组件,其中包括表单校验功能。在 Element UI 中,表单校验可以使用正则表达式来完成,这里的正则表达式就是 pattern。通过设置不同的 pattern 可以校验表单中输入的内容是否符合指定的格式要求。例如,如果你想要校验一个输入框中的内容是否为数字,你可以设置其 pattern 属性为 /^[0-9]*$/。这个正则表达式表示只能输入数字,不能输入其他字符。
需要注意的是,在使用 pattern 校验时,需要设置该输入框的 show-error 属性为 true,这样才能在校验失败时显示错误信息。
相关问题
element ui 表单手机号验证
Element UI 表单中可以使用自定义校验规则来验证手机号码的格式是否正确。可以使用正则表达式来进行校验。
以下是一个示例代码:
```html
<template>
<el-form :model="form" :rules="rules">
<el-form-item label="手机号码" prop="mobile">
<el-input v-model="form.mobile"></el-input>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
mobile: ''
},
rules: {
mobile: [
{ required: true, message: '请输入手机号码', trigger: 'blur' },
{ pattern: /^1\d{10}$/, message: '手机号码格式不正确', trigger: 'blur' }
]
}
};
}
};
</script>
```
在上面的示例中,我们使用了 `rules` 属性来定义校验规则,其中 `mobile` 是要校验的字段名。`required` 规则表示该字段是必填的,`pattern` 规则表示该字段必须匹配正则表达式 `^1\d{10}$`,即以数字 1 开头,后面跟着 10 个数字。
在表单提交时,如果手机号码格式不正确,会自动显示错误提示信息。
element ui 表单身份号码验证
Element UI 表单中可以使用自定义校验规则来验证身份证号码的格式是否正确。可以使用正则表达式来进行校验。
以下是一个示例代码:
```html
<template>
<el-form :model="form" :rules="rules">
<el-form-item label="身份证号码" prop="idCard">
<el-input v-model="form.idCard"></el-input>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
idCard: ''
},
rules: {
idCard: [
{ required: true, message: '请输入身份证号码', trigger: 'blur' },
{ pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '身份证号码格式不正确', trigger: 'blur' }
]
}
};
}
};
</script>
```
在上面的示例中,我们使用了 `rules` 属性来定义校验规则,其中 `idCard` 是要校验的字段名。`required` 规则表示该字段是必填的,`pattern` 规则表示该字段必须匹配身份证号码的正则表达式。
在表单提交时,如果身份证号码格式不正确,会自动显示错误提示信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)