// 只能輸入數字且只有一位小數 proving(item) { // this.form[item] 是input的值 // 先把非數字的都替換掉,除了數字和. this.form[item] = this.form[item].replace(/[^\d.]/g, ""); // 必須保證第一個為數字而不是. this.form[item] = this.form[item].replace(/^\./g, ""); // 保證只有出現一個.而沒有多個. this.form[item] = this.form[item].replace(/\.{2,}/g, ""); // 保證.只出現一次,而不能出現兩次以上 this.form[item] = this.form[item] .replace(".", "$#$") .replace(/\./g, "") .replace("$#$", "."); let index = -1; for (let i in this.form[item]) { if (this.form[item] === ".") { index = i; } if (index !== -1) { if (i - index > 1) { this.form[item] = this.form[item].substring( 0, this.form[item].length - 1 ); } } }
隨手保存下,下次方便使用