Decimal or numeric values in regular expression validation

前端 未结 10 1397
礼貌的吻别
礼貌的吻别 2020-11-22 15:09

I am trying to use a regular expression validation to check for only decimal values or numeric values. But user enters numeric value, it don\'t be first digit \"0\"

10条回答
  •  感动是毒
    2020-11-22 15:47

    I've tested all given regexes but unfortunately none of them pass those tests:

        String []goodNums={"3","-3","0","0.0","1.0","0.1"};
        String []badNums={"001","-00.2",".3","3.","a",""," ","-"," -1","--1","-.1","-0", "2..3", "2-", "2...3", "2.4.3", "5-6-7"};
    

    Here is the best I wrote that pass all those tests:

    "^(-?0[.]\\d+)$|^(-?[1-9]+\\d*([.]\\d+)?)$|^0$"
    

    enter image description here

提交回复
热议问题