1. 程式人生 > >input輸入限制(持續更新)

input輸入限制(持續更新)

pan only bmi pbo length 讀文本 num tom clipboard

1.只讀文本框內容

<!--  在input裏添加屬性值 readonly  -->
<input type="text" value="" readonly>

2.只能輸入數字(有閃動)

<input type="text" onkeyup="value=value.replace(/[^\d]/g,‘‘)" onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

3.只能輸入英文和數字

<input onkeyup
="value=value.replace(/[\W]/g,‘‘)" onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

4.只能輸入數字和小數點後兩位

<input type="text" onkeyup="clearNoNum(this)">

function clearNoNum(obj){
  obj.value = obj.value.replace(/[^\d.]/g,""); //清除"數字"和"."以外的字符
  obj.value = obj.value.replace(/^\./g,""); //
驗證第一個字符是數字而不是   obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一個. 清除多余的   obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");   obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,‘$1$2.$3‘); //只能輸入兩個小數
};

5.只能輸入整數

function zhengShu(obj){
  obj.value = obj.value.replace(/[^\d]/g,""); //
清除"數字"和"."以外的字符   obj.value = obj.value.replace(/^\./g,""); //驗證第一個字符是數字而不是 }

6.限制文本框輸入字數

<form name="form" action="" method="post">
  <textarea class="editbox2" onkeydown="numPic(this.form.memo,this.form.remLen,10)" onkeyup="numPic(this.form.memo,this.form.remLen,10)" name="memo" cols="45" rows="8" wrap="on"></textarea>
  <br>共可輸入10字,還剩
  <input class="editbox1" readOnly maxLength="3" size="3" value="10" name="remLen">字。
  <br>
  <input class="bottom" type="submit" value=" 提交 " name="submit">
  <input class="bottom" type="reset" value=" 重置 " name="reset">
</form>
function numPic(field, countfield, maxlimit) {
  if (field.value.length > maxlimit){
    field.value = field.value.substring(0, maxlimit);
  }else{
    countfield.value = maxlimit - field.value.length;
  }
};

input輸入限制(持續更新)