溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

js正則表達(dá)式控制文本框輸入浮點數(shù)

發(fā)布時間:2020-06-16 12:15:33 來源:網(wǎng)絡(luò) 閱讀:1008 作者:zpp0422 欄目:編程語言

//首先定義另個js方法 regInput 和  oncheckLength

//驗證輸入是否合法

function regInput(obj, reg, inputStr) {

   var docSel = document.selection.createRange()

   if (docSel.parentElement().tagName != "INPUT") return false

   oSel = docSel.duplicate()

   oSel.text = ""

   var srcRange = obj.createTextRange()

   oSel.setEndPoint("StartToStart", srcRange)

   var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)

   return reg.test(str)

}

//截取小數(shù)點后幾位小數(shù),

function oncheckLength(tag) {

   //小數(shù)位數(shù)只要修改  format的第一個參數(shù)就行

   tag.value = tag.value.trim().format(2, true);

}

   //input調(diào)用  如果數(shù)據(jù)輸入整數(shù)時 在光標(biāo)移開 調(diào)用oncheckLength方法自動填充小數(shù)點

   //如果不需要自動填充小數(shù)點后位數(shù),則取消onblur方法

   <asp:TextBox ID="txtVihiclePrice" runat="server" Width="70%" onkeypress="return regInput(this,/^\d*\.?\d{0,2}$/,String.fromCharCode(event.keyCode))"onblur="oncheckLength(this)"></asp:TextBox>


//實現(xiàn)效果:

   輸入:20   光標(biāo)移開顯示:20.00

   輸入:20.3 光標(biāo)移開顯示:20.30

   如果輸入小數(shù)點后超過2位則阻止輸入


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI