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

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

问题描述

我正在尝试在我的博客上创建一个日期文本框,而日期只是数字.我不希望任何人在输入字母时出错.有没有办法将字符限制为仅数字?谢谢!

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天全站免登陆