亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

驗證正整數和正小數,并且小數部分不能超過兩位,的js腳本!跪求

驗證正整數和正小數,并且小數部分不能超過兩位,的js腳本!跪求

_朝歌_ 2016-03-08 15:32:31
//這是一個驗證正整數包括小數的JS腳本。求同!//校驗輸入內容為實數function CheckNewReal(obj){ var parent = obj.parentNode; //清空錯誤信息,重新校驗 clearMassage(obj); var length = 20; var tempValue = obj.value;? var patten = /^-?\d+\.?\d*$/; if(tempValue.match(patten) && tempValue.length <= length){ return true; }else if(tempValue.length > length){ var massageObj=document.createElement("div"); massageObj.setAttribute("id","errorMassage"); massageObj.innerHTML="輸入的內容不能超過"+length+"個字符"; massageObj.style.color= "red"; parent.insertBefore(massageObj,obj); obj.style.color = "black";? ? ? ? obj.style.border = "1px solid #e50000";? ? ? ? return false; }else if(tempValue.length == 0){ obj.value = "0.0"; }else{ var massageObj=document.createElement("div"); massageObj.setAttribute("id","errorMassage"); massageObj.innerHTML="內容只能為整數或小數"; massageObj.style.color= "red"; parent.insertBefore(massageObj,obj); obj.style.color = "black";? ? ? ? obj.style.border = "1px solid #e50000";? ? ? ? return false; }}
查看完整描述

2 回答

已采納
?
慕郎_蓮華

TA貢獻83條經驗 獲得超16個贊

/^\d+\.*\d{0,2}$/ig.test(2)?//?true
/^\d+\.*\d{0,2}$/ig.test(2.0)?//?true
/^\d+\.*\d{0,2}$/ig.test(2.22)?//?true
/^\d+\.*\d{0,2}$/ig.test(2.222)?//?false

試試看~~勉強可以

查看完整回答
反對 回復 2016-03-09
  • _朝歌_
    _朝歌_
    謝謝你的回復,可以看看我的回答
?
_朝歌_

TA貢獻10條經驗 獲得超3個贊

/^[1-9]\d*$|^(\d|[1-9]\d*)\.\d{2}$/ig

查看完整回答
1 反對 回復 2016-03-09
  • 2 回答
  • 0 關注
  • 2136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號