js正则表达式验证金额
生活随笔
收集整理的這篇文章主要介紹了
js正则表达式验证金额
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
只允許輸入整數(shù)部分不超過六位數(shù),小數(shù)部分不超過2位,輸入金額不正確則禁止輸入,完整代碼
<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>js控制輸入金額</title> <script language="JavaScript" type="text/javascript" src="jquery.js"></script > <script language="JavaScript" type="text/javascript"> // 輸入金額整數(shù)部分不超過6位數(shù),不超過兩位小數(shù) $("#input1").on("input propertychange",function(event) {//禁止錄入整數(shù)部分兩位以上,但首位為0this.value = this.value.replace(/^([1-9]\d*(\.[\d]{0,2})?|0(\.[\d]{0,2})?)[\d.]*/g, '$1');//禁止錄入整數(shù)部分超過6位this.value = this.value.replace(/^([1-9]\d{5})\d*/g, '$1');//替換除了數(shù)字和.以外的字符this.value = this.value.replace(/[^\d\.]/g, '');//第一個(gè)輸入. 則自動(dòng)變?yōu)?.this.value = this.value.replace(/^\./g, '0.');//保證.只出現(xiàn)一次,而不能出現(xiàn)兩次以上 2.. 2.2.2this.value = this.value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');//只能輸入兩個(gè)小數(shù)this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); }) </script> </head> <body> <input id="input1"> </body> </html>兼容電腦端,安卓,ios
另:ios輸入法用九宮格的 .,: 鍵會(huì)有刪除的問題,暫未解決
總結(jié)
以上是生活随笔為你收集整理的js正则表达式验证金额的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php中文转化为英文,json转码_解决
- 下一篇: 高通骁龙712处理器规格参数介绍