Decimal or numeric values in regular expression validation

前端 未结 10 1269
礼貌的吻别
礼貌的吻别 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

    Here is my regex for validating numbers:

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

    Valid numbers:

    String []validNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};
    

    Invalid numbers:

    String []invalidNumbers={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};
    
    0 讨论(0)
  • 2020-11-22 15:47

    Try this code, hope it will help you

    String regex = "(\\d+)(\\.)?(\\d+)?";  for integer and decimal like 232 232.12 
    
    0 讨论(0)
  • 2020-11-22 15:54

    Here is a great working regex for numbers. This accepts number with commas and decimals.

    /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/
    
    0 讨论(0)
  • 2020-11-22 15:56

    /([0-9]+[.,]*)+/ matches any number with or without coma or dots

    it can match

             122
             122,354
             122.88
             112,262,123.7678
    

    bug: it also matches 262.4377,3883 ( but it doesn't matter parctically)

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