vue 指令 保留兩位小數 正數 不能爲負數

 directives: {
    num: {
      update: function (el) {
        let oinput = el.children[0].lastElementChild
        console.dir(oinput)
        if (oinput) {
          oinput.value = oinput.value.replace(/[^0-9|.]|\s/g, '')
          oinput.dispatchEvent(new Event('input'))
          oinput.onblur = function () {
            let reg = /(^[1-9]+\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
            if (!reg.test(oinput.value)) {
              oinput.value = ''
              oinput.dispatchEvent(new Event('input'))
            }
          }
        }
      }
    }
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章