不允许在 <input type=number/> 中输入字母字符. [英] Don't allow typing alphabetic characters in a <input type=number />

查看:21
本文介绍了不允许在 <input type=number/> 中输入字母字符.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个文本框,无论何时在其中输入,都应该只允许数字 [0-9].我使用了 type="number" ,它肯定拥有客户端验证,但也允许输入其他字母.我可以通过跟踪每个 keydown 并与正则表达式匹配来做到这一点,但我想知道是否有任何方法可以限制只使用 html 标签而不是在 JavaScript 中定义任何函数以在每个 keydown 中进行比较?

I need to have a textbox, where, whenever typed inside should ONLY allow numbers [0-9]. I've used type="number" which definitely holds the client side validation but also allows to type other alphabets. I can do it by tracking each keydown and matching with regex but I was wondering if there is any way I can restrict to type using only html tags and NOT defining any functions in JavaScript to compare in each keydown?

不足够的代码是:

    <input type="number" id="txt_number" maxlength="70" name="txt_name" placeholder="Number">

感谢任何帮助.

推荐答案

你可以使用 jquery.numeric 插件.

这里 类似的问题.

$(document).ready(function(){
   $(".numeric").numeric();
});

这篇关于不允许在 &lt;input type=number/&gt; 中输入字母字符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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