如何将文本区域限制为仅包含数字? [英] How to limit the textarea to only hold numbers?

查看:61
本文介绍了如何将文本区域限制为仅包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在博客上创建一个日期文本框,并且日期仅是数字.我不希望有人在输入字母时犯错误.有什么方法可以将字符限制为仅数字?谢谢!

I'm trying to make a date textbox on my blog, and the date is only numbers. I don't want anybody making a mistake typing a letter. Is there any way to limit the characters to only numerals? Thanks!

推荐答案

如果使用jQuery,则可以像此jsfiddle

If you use jQuery, you can do it like in this jsfiddle

$('input').keypress(function(e) {
    var a = [];
    var k = e.which;

    for (i = 48; i < 58; i++)
        a.push(i);

    if (!(a.indexOf(k)>=0))
        e.preventDefault();
});

这篇关于如何将文本区域限制为仅包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆