简体   繁体   English

HTML “货币”字段的输入模式

[英]HTML Input pattern for “currency” field

Attempting to use a regex pattern for an HTML input element that limits the characters to 0-9, ",","-",".","$"尝试对 HTML 输入元素使用正则表达式模式,将字符限制为 0-9、“、”、“-”、“.”、“$”

I have very little experience in regex and used the pattern=[0-9,.-$] on the input element and it does not work.我在正则表达式方面的经验很少,并且在输入元素上使用了 pattern=[0-9,.-$] 并且它不起作用。 Though I plan on studying regex intensely in the future, I need a little help on this for now.虽然我计划在未来深入研究正则表达式,但我现在需要一点帮助。 Thank you.谢谢你。

 <form> <input name="currency" pattern=[0-9,.-$]> <button>Submit</button> </form>

don't use regex, use native type="number" that will do job for you with no need for regex不要使用正则表达式,使用本机type="number"这将为您完成工作,而无需正则表达式

coverage is almost full > https://caniuse.com/#feat=input-number覆盖范围几乎已满 > https://caniuse.com/#feat=input-number

 <form> <input type="number" name="currency"> <button>Submit</button> </form>

You have two problems.你有两个问题。

  • - indicates a range , you already used it as such for 0-9 . -表示一个范围,您已经将它用于0-9 .-$ isn't a valid range. .-$不是有效范围。 A validator would have highlighted this for you.验证者会为您突出显示这一点。 You need to escape the - .你需要逃避-
  • You are only matching one character.您只匹配一个字符。 You need to match multiple so use something like + to mean one or more您需要匹配多个,因此使用+表示一个或多个

 <form> <input name="currency" pattern="[0-9,.\-$]+"> <button>Submit</button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM