限制input輸入符合規則的浮點數

2021-07-09 04:29:58 字數 1957 閱讀 9311

//給指定的dom新增事件

$('#col-sm-2, #min_goods_amount').keydown(function(e));

/*** 限制input輸入符合規則的浮點數

* * @param e 鍵盤事件

* @param maxint 整數字最大位數 預設 6位整數

* @param maxdecimal 小數字最大位數 預設 2位整數

* @returns

*/function formatinput(e, maxint, maxdecimal)}}

//限制浮點數規則

maxint = maxint || 6;

maxdecimal = maxdecimal || 2;

e.keycode = e.keycode || e.charcode;

//當前的值

var val = $(e.currenttarget).val();

//輸入的值

var input = string.fromcharcode(e.keycode);

//小數點的位置

var pointposition = val.indexof('.');

//游標的位置

var seletorposition = getcursorposition( e.currenttarget );

//判斷小數點的變數

var n = val.split('.');

//上下左右鍵

if(e.keycode >=37 && e.keycode <=40)

//輸入的是退格鍵

if(e.keycode == 8)

//如果有小數點,如果整數字加上小數字的位數大於最大整數字,這時不讓刪除小數點,以免出現規則外的整數字

if(pointposition !=-1 && n[0].length + n[1].length > maxint && (pointposition + 1) == seletorposition)

return true;

}//delete 鍵

if( e.keycode == 46 )

//如果有小數點,如果整數字加上小數字的位數大於最大整數字,這時不讓刪除小數點,以免出現規則外的整數字

if(pointposition !=-1 && n[0].length + n[1].length > maxint && (pointposition) == seletorposition)

return true;

}//input中已經有值,則不允許在第乙個位置輸入0

if(e.keycode == 48 && seletorposition==0 && val != '')

//輸入的是小數點

if(e.keycode == 190 || e.keycode == 46)

//輸入的不是數字

if( e.keycode < 48 || e.keycode > 57 )

//游標在第一位,整數字是0 ,這是輸入數值是直接替換0

if(seletorposition == 1 && parseint(n[0]) == 0)else

setseletorposition(e.currenttarget, 1);

return false;

}//如果有小數點

if(pointposition > 0)

}//整數字的位數

return n[0].length < maxint;

}//獲得游標所在input的位置

function getcursorposition(dom)else

return cursurposition;

}//設定游標所在input的位置

function setseletorposition(dom, index) else

}

js限制input輸入

1.取消按鈕按下時的虛線框,在input裡新增屬性值 hidefocus 或者 hidefocus true 2.唯讀文字框內容,在input裡新增屬性值 readonly 3.防止退後清空的text文件 可把style內容做做為類引用 4.enter鍵可以讓游標移到下乙個輸入框 5.只能為中文 有...

js限制input輸入

1.取消按鈕按下時的虛線框,在input裡新增屬性值 hidefocus 或者 hidefocus true 2.唯讀文字框內容,在input裡新增屬性值 readonly 3.防止退後清空的text文件 可把style內容做做為類引用 4.enter鍵可以讓游標移到下乙個輸入框 5.只能為中文 有...

js限制input輸入

1.取消按鈕按下時的虛線框,在input裡新增屬性值 hidefocus 或者 hidefocus true 2.唯讀文字框內容,在input裡新增屬性值 readonly 3.防止退後清空的text文件 可把style內容做做為類引用 4.enter鍵可以讓游標移到下乙個輸入框 5.只能為中文 有...