Javascript function need allow numbers, dot and comma

前端 未结 6 1961
悲&欢浪女
悲&欢浪女 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:31

    No need for JavaScript:

    
    

    Modern browsers will handle this perfectly.

    If you want to specifically allow commas as thousand separators and a single decimal point, try this:

    ... pattern="\d{1,2}(,\d{3})*(\.\d+)?" ...
    

    Note that I am firmly against blocking user input. They should be able to type what they want, and then told if they enter something invalid.

提交回复
热议问题