Regular Expression allow null or 1 to 9 digit

后端 未结 3 1841
被撕碎了的回忆
被撕碎了的回忆 2021-01-26 07:08

I have tried to validate my input quantity field by using pattern attribute. But my regular expression not working. Please check

^(?:[1-9]\\d*|)$
相关标签:
3条回答
  • 2021-01-26 07:32

    You may check by yourself:

    https://regex101.com^(?:[1-9]\d*|)$

    0 讨论(0)
  • 2021-01-26 07:40
    ^(?:[1-9]*)$
    

    Should be enough.

    0 讨论(0)
  • 2021-01-26 07:41

    If you want to match multiple numbers 1-9 use:

    ^[1-9]*$
    

    This regex is as good as it gets with regard to checking for NULL values. If you want to also allow nulls, then you should include this logic explicitly in your controller, i.e.

    form.input.$valid || quoteList.quantity === null
    

    Update:

    It appears that what you really wanted to ask is how to validate a number which begins with 1-9, but then can be followed by any digit. If so, then try this regex:

    ^[1-9][0-9]*$
    

    Again, you can check in your controller for null values and also allow them too.

    0 讨论(0)
提交回复
热议问题