• el-input一些校验 & 事件


    目录

    1、el-input 限制输入纯数字和小数点。且输入不为0

    2、限制el-input小数点后只能跟两位

    3、el-select 创建条目 动态查找 & 鼠标移出选择框中仍有值(无需选中或回车)

    4、手机号,必填且有校验

    5、el-input,不必填,但有是否输入正确的校验

    6、el-form清空校验


    1、el-input 限制输入纯数字和小数点。且输入不为0
    1. "submitGatheringForm" :model="submitGatheringForm" :rules="formIndustryules" label-width="120px" size="small">
    2. <el-form-item label="金额" prop="amount">
    3. <el-input v-model="submitGatheringForm.amount" type="number" @blur="blurAmount" placeholder="请输入金额">el-input>
    4. el-form-item>
    5. methods: {
    6. blurAmount(val) {
    7. if (this.submitGatheringForm.amount * 1 == 0) {
    8. this.submitGatheringForm.amount = '';
    9. }
    10. },}

    或者

    1. "price" type="text" @blur="blurRowPrice">
    2. blurRowPrice() {
    3. var r = /\d+(\.\d+)?$/;
    4. var flag = r.test(this.price * 1);
    5. if (flag) {
    6. if (this.price * 1 == 0) {
    7. this.price = '';
    8. }
    9. } else {
    10. this.price= '';
    11. }
    12. },

     

    2、限制el-input小数点后只能跟两位
    1. "basicInfoForm" :model="basicInfoForm" :rules="formIndustryules" label-width="120px" size="small">
    2. <el-form-item label="金额" prop="amount">
    3. <el-input v-model="submitGatheringForm.amount" @input="handleInput">el-input>
    4. el-form-item>
    5. methods: {
    6. handleInput(value) {
    7. this.basicInfoForm.creditLine =
    8. this.basicInfoForm.creditLine
    9. .replace(/[^\d^\.]+/g, '')
    10. .replace(/^0+(\d)/, '$1')
    11. .replace(/^\./, '0.')
    12. .match(/^\d*(\.?\d{0,2})/g)[0] || '';
    13. // this.basicInfoForm.creditLine = value.replace(/[^\d\.]/g, '');
    14. },
    3、el-select 创建条目 动态查找 & 鼠标移出选择框中仍有值(无需选中或回车)
    1. "物流公司" prop="logisticsName">
    2. <el-select v-model="overallShipmentForm.logistics" @blur="blurComLog" filterable remote :remote-method="remoteMethod" :loading="loading" @change="selectChanged" placeholder="请输入选择物流公司" allow-create default-first-option size="small">
    3. <el-option v-for="item in overallShioptions" :key="item.value" :label="item.label" :value="item"> el-option>
    4. el-select>
    5. <script>
    6. export default {
    7. props: [],
    8. data() {
    9. return {
    10. overallShipmentForm: {
    11. logistics: {},
    12. logisticsCode: '',
    13. logisticsName: '',
    14. },
    15. overallShioptions: [], //下拉选项
    16. },
    17. },
    18. methods: {
    19. // 动态查找
    20. async remoteMethod(query) {
    21. if (query != '') {
    22. this.loading = true;
    23. // 根据输入框调接口查找是否有符合条件的,如果有就赋值overallShioptions
    24. let data = (await getoverallShioptions(query ).data;
    25. var list = data.map(item => {
    26. return {
    27. value: `${item.code}`,
    28. label: `${item.name}`,
    29. };
    30. });
    31. setTimeout(() => {
    32. this.loading = false;
    33. this.overallShioptions = list;
    34. }, 200);
    35. } else {
    36. this.overallShioptions = [];
    37. }
    38. },
    39. //用户输入后,移出输入框,el-input中仍有值,无需选中或回车
    40. blurComLog(e) {
    41. let value = e.target.value; // 输入框值
    42. console.log(value);
    43. if (value) {
    44. // 你输入才有这个值 不为空,如果你下拉框选择的话 这个值为空
    45. this.overallShipmentForm.logistics = value;
    46. this.overallShipmentForm.logisticsName = value;
    47. this.overallShipmentForm.logisticsCode = '';
    48. }
    49. },
    50. script>
    4、手机号,必填且有校验
    1. data() {
    2. let validatete = (rule, value, callback) => {
    3. let valimessage = validatePhone(this.basicInfoForm.mobile);
    4. if (valimessage !== '') {
    5. callback(new Error(valimessage));
    6. } else {
    7. callback();
    8. }
    9. };
    10. return {
    11. basicInforules: {
    12. mobile: [
    13. { required: true, message: '请输入手机号码', trigger: 'blur' },
    14. { validator: validatete, trigger: 'blur' },
    15. ],
    16. },
    17. };
    18. },
    5、el-input,不必填,但有是否输入正确的校验
    1. data() {
    2. return {
    3. basicInforules: {
    4. email: [
    5. { required: false, message: '请输入邮箱!', trigger: 'blur' },
    6. {
    7. validator: (rule, value, callback) => {
    8. const regExp = new RegExp(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/);
    9. console.log(this.basicInfoForm.email);
    10. if (this.basicInfoForm.email == '' || this.basicInfoForm.email == undefined) {
    11. callback();
    12. } else {
    13. if (!regExp.test(this.basicInfoForm.email)) {
    14. callback(new Error('邮箱格式不正确'));
    15. } else {
    16. callback();
    17. }
    18. }
    19. },
    20. trigger: 'blur',
    21. },
    22. ],
    23. },
    24. };
    25. },
    6、el-form清空校验
    1. // 清除el-form所有表单内容,清空校验
    2. this.$refs.formIndustrydata.resetFields();
    3. // 清除校验
    4. this.$refs.formIndustrydata.clearValidate();
    5. // 清除对name的校验校验
    6. this.$refs.formIndustrydata.clearValidate('name');
    7. //如果不生效加上nextTick
    8. this.$nextTick(() => {
    9. this.$refs.formIndustrydata.clearValidate();
    10. });

  • 相关阅读:
    mysql面试题8:MySQL的日志有哪些?MySQL的bin log、redo log、undo log区别和联系
    基于Matlab构建适用于无人机或四轴飞行器的IMU+GPS融合算法(附源码)
    group by 常量执行耗时长
    1024——HelloWorld
    029-从零搭建微服务-消息队列(一)
    JS38 高频数据类型
    ImGUI 1.87 绘制D3D外部菜单
    C++入门学习4-指针与内存分配,引用
    【JUC基础】11. 并发下的集合类
    图像处理之《基于多MSB预测和Huffman编码的加密图像可逆数据隐藏》论文精读
  • 原文地址:https://blog.csdn.net/m0_69502730/article/details/134244482