允许在HTML表单上输入“+”号码=“number” [英] Allow '+' sign on HTML form type="number"

查看:247
本文介绍了允许在HTML表单上输入“+”号码=“number”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,如果我将表单的类型设置为数字,则HTML不会接受带加号(+12)的数字作为输入。但它确实接受带负号的数字(-12)。是否有可能让它接受加号?

解决方案

+12 不是数字,但是 -12 是。您唯一的选择是将其更改为 type =text。无论如何你都不能相信前端验证,并且总是有其他方法(正则表达式等)来验证输入中的字符串是一个数字。这个例子是HTML 5,只支持IE 10+(与 type =number相同)。



 <形式> < input type =textpattern =[ -  +]?\d */> < input type =submitvalue =Test/>< / form>  

b

So if I set the type of a form to number, HTML does not accept as input a number with the plus sign (+12). But it does accept a number with the negative sign (-12). Is it possible to make it accept the plus sign?

解决方案

+12 is not a number, but -12 is. Your only option is changing it to type="text". You can't trust frontend validation anyways and there are always other methods (regex, etc) to verifying that the string in an input is a number. This example is HTML 5 and only supported on IE 10+ (same as type="number").

<form>
    <input type="text" pattern="[-+]?\d*" />
    <input type="submit" value="Test" />
</form>

这篇关于允许在HTML表单上输入“+”号码=“number”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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