教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 基于jquery的设置页面文本框 只能输入数字的实现代码

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

发布时间:2013-07-06   编辑:jiaochengji.com
之前写过的方法有缺陷,可以输入空格。现在将空格也屏蔽了。就是在之前的代码里加入了过滤空格的功能。
之前写过的方法有缺陷,可以输入空格。现在将空格也屏蔽了。就是在之前的代码里加入了过滤空格的功能。 代码如下:
复制代码 代码如下:

$("#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;});
}

您可能感兴趣的文章:
jQuery 自动增长的文本输入框实现代码
基于jquery的设置页面文本框 只能输入数字的实现代码
基于jquery的一个简单的脚本验证插件
jqPlot jquery的页面图表绘制工具
基于jQuery的输入框无值自动显示指定数据的实现代码
基于jquery的bankInput银行卡账号格式化
StringTemplate遇见jQuery冲突的解决方法
基于jQuery的输入框在光标位置插入内容, 并选中
jquery easyui 分页的使用方法
ASP.NET jQuery 实例13 原创jQuery文本框字符限制插件-TextArea Counter

[关闭]
~ ~