Javascript regular expression for negative Numbers with decimal

前端 未结 8 1381
感动是毒
感动是毒 2021-01-05 05:20

I want to test for this input: [an optional negative sign] 2 digits [an optional . and an optional digit] like this:

-34 or -34.5333 or

8条回答
  •  [愿得一人]
    2021-01-05 06:01

    Perhaps regex is not needed.

    function validate(val){
        return isNaN(val)?false:(Math.abs(Math.floor(val)).toString().length==2);
    }
    

提交回复
热议问题