file写法:
// 需要引入
import { Toast, Dialog, Notify } from 'vant'
<van-form>
<van-cell-group inset>
<van-field :border="false" :rules="carNumberRules" v-model="carNumber" name="车牌号" label="车牌号" placeholder="请输入车牌号" />
</van-cell-group>
<div style="margin: 16px;" class="optionBtn_box">
<van-button round block type="primary" @click="onSubmit">
确定
</van-button>
</div>
</van-form>
rule的校验规则
private carNumberRules: any[] = [
{
pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使]{1}[A-HJ-NP-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/,
message: '请输入正确的车牌1号'
}
]
//点击确定时的校验
private validateCarCode() {
let carvalidate = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使]{1}[A-HJ-NP-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
if (carvalidate.test(this.carNumber)) {
return true
} else {
return false
}
}
private onSubmit(values) {
if (this.validateCarCode()) {
//里面写通过的内容
} else {
Notify({ type: 'danger', message: '车牌号格式错误!' })
return
}
}