博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS 文本框格式化
阅读量:4480 次
发布时间:2019-06-08

本文共 1591 字,大约阅读时间需要 5 分钟。

页面:

<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>

<script src="inputFormat.js"></script>

<script type="text/javascript">

$(function(){
$('#account').inputFormat('account')
$('#money').inputFormat('amount')
})
</script>

<body>

帐号:<input id="account"/><br/>

金额:<input id="money"/>

</body>

inputFormat.js

 

(function($){

var methods={
init:function(){
$.error('What are you doing?')
},
account:function() {
$(this).keydown(function(event){
var code=event.keyCode
if (event.shiftKey){
return false
}
if((code>=96&&code<=105)||((!event.shiftKey)&&code>=48&&code<=57)||code<57){
return true
}
return false
})
$(this).keyup(function(){
$(this).val($(this).val().replace(/\s(?=\d)/g,'').replace(/(\d{4})(?=\d)/g,"$1 "))
})
},
amount:function(){
$(this).keydown(function(event){
var code=event.keyCode
var value=$(this).val()
if (event.shiftKey||code==32){
return false
}
if((code>=96&&code<=105)||((!event.shiftKey)&&code>=48&&code<=57)||code<57||code==110||code==190){
return true
}
return false
})
$(this).keyup(function(event){
var value=$(this).val().replace(/\,|\s/g,'')
if(value!=''){
$(this).val(value.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,").replace(/\.00$/,''))
}
})
}

}

$.fn.inputFormat=function(method){
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}else {
$.error('Method ' + method + ' does not exist on jQuery.inputFormat');
}
}
})(jQuery);

 

 

转载于:https://www.cnblogs.com/lenovo_tiger_love/p/3739771.html

你可能感兴趣的文章
洛谷——P1602 Sramoc问题
查看>>
【MySQL笔记】字符串、时间日期转换
查看>>
jQuery实战之仿淘宝商城左侧导航效果
查看>>
AC日记——「SCOI2016」幸运数字 LiBreOJ 2013
查看>>
unmount
查看>>
数据库连接池
查看>>
javascript获得和设置以及移除元素属性的三个方法
查看>>
windwos iis 7.5 使用html 报405错误
查看>>
范围(地址转换)
查看>>
Unity3D游戏,TCP,WEBCOSKT,HTTP通信架构 weaving-socket
查看>>
【小程序入门集锦】19,微信小程序个人帐号申请
查看>>
php写一个简单的计算器
查看>>
【JAVA零基础入门系列】Day3 Java基本数据类型
查看>>
两个整数,求他们的最小公倍数和最大公约数
查看>>
Mongo索引
查看>>
php 实现设计模式之 建造者模式
查看>>
An Easy C Program Problem
查看>>
Replace Nested Conditional with Guard Clauses(用卫语句代替嵌套循环)
查看>>
jsp中${}是EL表达式的常规表示方式
查看>>
GoldenGate常见问题及处理
查看>>