咱們在作表單輸入時,有時候對於有些輸入比較有限制,好比輸入天數必須爲正整數,再好比有些特殊需求須要輸入保留小數點的後面n位。那麼咱們如何在輸入環節就限制用戶的輸入狀況呢?javascript
咱們能夠用正則表達式來限制。java
<input type="number" class="weight-input" oninput="this.value=this.value.replace(/\D/g,'');" pattern="[0-9]*"> 天
解析
ios
<input type="number" class="weight-input" min="1" oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/,'')}else{this.value=this.value.replace(/\D/g,'')}" pattern="[1-9][0-9]*"> 天
解析
正則表達式
<input type="number" class="weight-input" min="0.000" oninput="this.value=this.value.replace(/\D*(\d*)(\.?)(\d{0,3})\d*/,'$1$2$3')" pattern="[0-9]*\.?[0-9]{0,3}">