美文网首页
正则匹配输入框保留两位小数限制最多位数

正则匹配输入框保留两位小数限制最多位数

作者: 幸宇 | 来源:发表于2020-09-01 16:21 被阅读0次
    <template>
      <div class="hello">
    
        <el-form :model="ruleForm" :rules="ruleValidate">
    
          <el-form-item prop="number">
              <el-input labe="输入数字" v-model="ruleForm.number" placeholder="请输入数字" ></el-input>
          </el-form-item>
    
        </el-form>
    
        <h1>{{ msg }}</h1>
        
      </div>
    </template>
    
    <script>
    export default {
      name: 'HelloWorld',
      data () {
        let checkPrice = (rule,value,callback)=>{
            if(value){
              // let rgx = /^\d+(\.\d{1,2})?$/;
              let rgx = /^[1-9]{1}\d{0,16}(\.\d{1,2})?$|^0(\.\d{1,2})?$/;
              if(value.match(rgx)==null){
                return callback(new Error('请检查输入格式,不能为空,且最多2位小数'))
              }else{
                callback();
              }
            }
          };
        return {
          msg: 'Welcome to Your Vue.js App',
          ruleForm:{
            number:''
          },
          ruleValidate:{
            number:[
              { required: true, message: '请输入数字', trigger: 'change' },
              { validator:checkPrice,trigger:'change'}
            ]
          }
        }
      }
    }
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped>
    h1, h2 {
      font-weight: normal;
    }
    ul {
      list-style-type: none;
      padding: 0;
    }
    li {
      display: inline-block;
      margin: 0 10px;
    }
    a {
      color: #42b983;
    }
    </style>
    
    

    相关文章

      网友评论

          本文标题:正则匹配输入框保留两位小数限制最多位数

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