Restricting text box inputs to a given regexp using jQuery

前端 未结 4 2026
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 07:23

Consider the following text box:


Using jQuery I want to restrict the

4条回答
  •  一向
    一向 (楼主)
    2020-12-20 08:18

    Not an answer to your question (since I have no knowledge of jQuery), but the regex ^[1-9]*[0-9]*$ might not do as you expect or think. It matches empty strings but also a number like 000000. I expect that you don't want that since your first character class [1-9] explicitly ignores the zero. I think you should change the first * into a +: ^[1-9]+[0-9]*$. However, that regex will reject the (single) number 0. If you want to include that, you should do: ^(0|[1-9]+[0-9]*)$.

提交回复
热议问题