美文网首页
vue中input校验方法

vue中input校验方法

作者: 生擒一条鳄鱼 | 来源:发表于2017-02-13 18:44 被阅读3082次

整理工作中用到的input验证问题,不定期更新。

  1. 正整数1
输入正整数<input type="text" name="" value="" v-on:keyup="check_count($event)">

对应的方法:
通过$event获取event对象

check_count: function(event) {
      var value = event.target.value;
      if (!/^\+?[1-9][0-9]*$/.test(value)) {
        alert('只能正整数');
        event.target.value = '';
      }
    },
  1. 正整数2
输入正整数<input type = "text" name= "number" id = 'number'
onkeyup= "if(!/^\+?[1-9][0-9]*$/.test(this.value)){alert('只能整数');this.value='';}" />
  1. 电话号码
电话号码<input type="text" name="" value="" v-model="phone_num">
<input type="button" name="" value="检查电话号码格式" v-on:click="check_phone()">

对应方法:

check_phone() {
      if (!/^1[34578]\d{9}$/.test(this.phone_num)) {
        alert("格式错误");
        this.phone_num = "";
      } else {
        alert("格式正确");
      }
    }

参考材料

http://www.jb51.net/article/83398.htm

相关文章

网友评论

      本文标题:vue中input校验方法

      本文链接:https://www.haomeiwen.com/subject/zvktwttx.html