Javascript function need allow numbers, dot and comma

前端 未结 6 1949
悲&欢浪女
悲&欢浪女 2020-12-09 04:30

I need to create a function to allow numbers dot and comma. So anyone corrects the following function

function on(evt) {
  var theEvent = evt || window.event         


        
6条回答
  •  [愿得一人]
    2020-12-09 05:10

    Firstly your regex currently doesn't allow comma, which is your requirement.

    Secondly, you haven't used any quantifier, so your regex will match only a single character - one of [0-9] or a dot. You need to use a quantifier.

    Thirdly, instead of using pipe, you can move all characters inside the character class only.

    Try using the below regex:

    /^[0-9.,]+$/
    

    Quantifier + is used to match 1 or more occurrence of the pattern.
    ^ and $ anchors match the beginning, and end of the string respectively.

提交回复
热议问题