vue系列:vue中使⽤vee-validate3表单验证vee-validate 可以说是 vue ⾥表单验证 star 最⾼的了,今天来实践⼀下具体怎么⽤。
dom代码
npm install vee-validate --save
我安装下来是 3.3.0 版本
<ValidationObserver ref="form">
<ValidationProvider rules="required"name="姓名"v-slot="{ errors }">
<div class="form-part username">
<label for="">姓名</label>
<input type="text"
class="form-input"
maxlength="20"
v-model="formData.username"
placeholder="请输⼊姓名">
</div>
<span class="vee-error">{{ errors[0] }}</span>
</ValidationProvider>
<ValidationProvider rules="required|isPhone"name="⼿机号"v-slot="{ errors }">
<div class="form-part phone">
<label for="">⼿机号</label>
<input type="text"
class="form-input"
v-model="formData.phone"最耐玩的电脑单机游戏
placeholder="请填写⼿机号">
</div>
<span class="vee-error">{{ errors[0] }}</span>
</ValidationProvider>
</ValidationObserver>
vuets 代码,vuejs同理
<script lang="ts">
// 让vue可以⽤ts
劫怎么玩import{ Vue, Component }from'vue-property-decorator'
// 引⼊vee-validate
import{ ValidationProvider, ValidationObserver, extend, localize }from'vee-validate' // 引⼊验证规则,可以⾃定义,如下的isPhone
import{ required }from'vee-validate/dist/rules'
2023年全国高速路免费时间表
//
import zhCN from'vee-validate/dist/locale/zh_CN.json'
interface FormDataInter {
username: string;
province: string;
city: string;
hospital: string;
phone: string;
}
//
localize('zh_CN', zhCN)
/
/ 引⼊验证规则
extend('required', required)
// ⾃定义验证规则
团结就是力量歌词extend('isPhone',{
validate: value =>{
const isPhone: boolean =/^[1][0-9]{10}$/.test(value)
return isPhone
},
message:'⼿机号格式不正确'
})
// 引⼊验证组件
三位数除以两位数的计算题
@Component({
components:{
ValidationProvider,
ValidationObserver
}
})
export default class extends Vue {
private formData: FormDataInter ={
username:'',
province:'',
city:'',
hospital:'',
种子的传播phone:''
}
// 点击提交按钮触发
private submit():void{
// 提交时候出发验证
// eslint-disable-next-line
(this.$refs.form as any).validate().then((success: boolean)=>{
if(success){
console.log(this.formData)
}
})
}
}
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论