Validating Positive number with comma and period

I need a regular expression validation expression that will

ALLOW

  • positive number(0-9)
  • , and .
  • DISALLOW

  • letter(az)
  • any other letter or symbol except . and ,
  • for example, on my asp.net text box, if I type anything@!#-- , the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.

    I've been trying to use this regex:

    ^d+(.dd)?$
    

    but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol


    Your regex would be,

    (?:d|[,.])+
    

    OR

    ^(?:d|[,.])+$
    

    It matches one or more numbers or , or . one or more times.

    DEMO


    Don't Use d to match [0-9] in .NET

    First off, in .NET, d will match any digits in any script, such as:

    654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
    

    So you really want to be using [0-9]

    Incomplete Spec

    You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$ , and that would match

    ...,,,
    

    See demo.

    Tweaking the Spec

    It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?

    We could start with something like this, to get some fairly well-formed strings:

    ^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:.[0-9]{3})*|(?:,[0-9]{3})*)(?:.[0-9]+)?)$
    

    Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.

    Sample Matches:

    0
    12
    12.123
    12,12
    12,123,123
    12,123,123.12456
    12.125.457.22
    

    Sample Non-Matches:

    12,
    123.
    1,1,1,1
    

    Maybe you can use this one (starts with digit, ends with digit):

    (d+[,.])*d+
    

    If you need more sophisticated price Regex you should use:

    (?:(?:[1-9]d?d?([ ,.]?d{3})*)|0)(?:[.,]d+)?
    

    Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:

    (^|s)(?:(?:[1-9]d?d?([ ,.]?d{3})*)|0)(?:[.,]d+)($|s)?
    
    链接地址: http://www.djcxy.com/p/86998.html

    上一篇: 正则表达式正好6个字符,首先必须是一个字母

    下一篇: 用逗号和句点验证正数