如何将文本区域限制为仅保存数字?

2022-01-17 00:00:00 numbers limit html textarea

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

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

相关文章