input設置只能輸入數字,附上手機號碼驗證


//1、input標簽設置只能輸入數字

//input標簽上加上 oninput="value=value.replace(/[^0-9.]/g,'')",如
  <el-input v-model="a" oninput="value=value.replace(/[^0-9.]/g,'')"></el-input>

//2、在只能輸入數字的基礎上,設置手機號驗證

<template>
  <el-input v-model="a" onblur="vaildPhone" oninput="value=value.replace(/[^0-9.]/g,'')"></el-input>
</template>
<script>
export default {
  data() {
    return {
      a: 1,
    };
  },
  methods: {
    testPhone(str) {
      const reg = /^1[3|4|5|7|8|6|9][0-9]\d{8}$/;
      return reg.test(str);
    },
    vaildPhone() {
      if (!this.testPhone(this.a)) {
        alert("手機號錯誤");
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM