首頁 > web前端 > js教程 > 主體

基于jquery的设置页面文本框 只能输入数字的实现代码_jquery

WBOY
發布: 2016-05-16 18:07:37
原創
909 人瀏覽過

代码如下:

复制代码 代码如下:

$("#money").bind("propertychange",function() {
if(""!=this.value){
var str = this.value.replace(/(^\s*)|(\s*$)/g, "");
if(this.value != str )
this.value = str;
}
if( isNaN(Number(this.value)))
this.value = this.value.replace(/[\D]/,'');
});

这里使用了JQuery绑定到id为money的文本框的onpropertychange事件上。
下面的代码连小数点也屏蔽掉了
复制代码 代码如下:

$("#phone").bind("propertychange", function() {
if(""!=this.value){
var str = this.value.replace(/(^\s*)|(\s*$)/g, "");
if(this.value != str )
this.value = str;
}
if (this.value.indexOf('.') != -1) {
this.value = this.value.replace(/[\.]/, '');
this.focus(); }
if (isNaN(Number(this.value))) {
this.value = ($.trim(this.value)).replace(/[\D]/, '');
this.focus(); } });

最后,最好将输入法屏蔽掉。 通过css,ime-mode:disabled就可以实现。
如果很严格的话,可以再追加上禁止粘贴与拖拽。
禁止粘贴与拖拽实现方法
文本框禁止拖拽和粘贴

在css中实现文本框禁止拖拽和粘贴的功能

建立一个Css,如下:
复制代码 代码如下:

.TextBox_NotDragpaste

{
ondragenter:expression(ondragenter=function(){return false;});
onpaste:expression(onpaste=function(){return false;});
}

如果还需要禁止输入中文的功能只需要多加一个语句即可。

如下:
复制代码 代码如下:

.TextBox_NotDragpaste

{
ime-mode:disabled;
ondragenter:expression(ondragenter=function(){return false;});
onpaste:expression(onpaste=function(){return false;});
}
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!