HTML5输入类型编号不能阻止加号,减号 [英] Html5 input type number not prevent plus, minus symbols

查看:66
本文介绍了HTML5输入类型编号不能阻止加号,减号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用输入类型数字,它可以防止字符,但是不能防止正号,负号或字符.但是我需要防止所有符号.

I am using input type number and It's preventing character but it's unable to prevent plus, minus symbol or character. But I require to prevent all symbol.

<input type="number">

我已经进行了处理,并创建了自定义正则表达式,并且可以正常工作,但是我想知道为什么输入类型编号可以正常工作,以及如何解决该问题.

I have worked on it and create my customise regex and it's working fine but I want to know why it's working like that for input type number and how we can fix that.

推荐答案

您可以使用 jquery

$('input').keypress(function (e) {
    var txt = String.fromCharCode(e.which);
    if (!txt.match(/[0-9]/)) {
        return false;
    }
});

这篇关于HTML5输入类型编号不能阻止加号,减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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