jquery 限制文本框只能输入数字

亦凉 2022-06-13 09:25 447阅读 0赞

http://blog.csdn.net/suntanyong88/article/details/38682703

[html] view plain copy

print ?

  1. jquery 限制文本框只能输入数字
  2. 转自:http://jonsion.iteye.com/blog/558277
  3. $(“input[name=’fangwenyudinhuishu’]“).keyup(function(){
  4. var tmptxt=$(this).val();
  5. $(this).val(tmptxt.replace(/\D|^0/g,’’));
  6. }).bind(“paste”,function(){
  7. var tmptxt=$(this).val();
  8. $(this).val(tmptxt.replace(/\D|^0/g,’’));
  9. }).css(“ime-mode”, “disabled”);
  10. 上面是keyup事件处理,下面处理了CTR+V事件,最后就是CSS设置输入法不可用

[html] view plain copy

print ?

  1. 用jquery限制文本框只能输入数字:
  2. 程序:
  3. $(function(){
  4. //文本框只能输入数字,并屏蔽输入法和粘贴
  5. $.fn.numeral = function() {
  6. $(this).css(“ime-mode”, “disabled”);
  7. this.bind(“keypress”,function(e) {
  8. var code = (e.keyCode ? e.keyCode : e.which); //兼容火狐 IE
  9. if(!$.browser.msie&&(e.keyCode==0x8)) //火狐下不能使用退格键
  10. {
  11. return ;
  12. }
  13. return code >= 48 && code<= 57;
  14. });
  15. this.bind(“blur”, function() {
  16. if (this.value.lastIndexOf(“.”) == (this.value.length - 1)) {
  17. this.value = this.value.substr(0, this.value.length - 1);
  18. } else if (isNaN(this.value)) {
  19. this.value = “”;
  20. }
  21. });
  22. this.bind(“paste”, function() {
  23. var s = clipboardData.getData(‘text’);
  24. if (!/\D/.test(s));
  25. value = s.replace(/^0*/, ‘’);
  26. return false;
  27. });
  28. this.bind(“dragenter”, function() {
  29. return false;
  30. });
  31. this.bind(“keyup”, function() {
  32. if (/(^0+)/.test(this.value)) {
  33. this.value = this.value.replace(/^0*/, ‘’);
  34. }
  35. });
  36. };
  37. //调用文本框的id
  38. $(“#score”).numeral();
  39. });

发表评论

表情:
评论列表 (有 0 条评论,447人围观)

还没有评论,来说两句吧...

相关阅读