Results 1 to 6 of 6

Thread: problem in validation fix number and decimal numbers

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    problem in validation fix number and decimal numbers

    hi
    I use following regular expression to validate my numbers:
    Code:
    ^[-]?\d{1,15}(\.\d{1,3})?
    my validate numbers is :

    Code:
    0 to 9 max length <= 15
    decimal numbers are optional and decimal place is <= 3 example : 1234.123
    0.123
    my invalidate numbers is :

    Code:
    0 to 9 max length > 15
    decimal numbers are optional and decimal place is > 3 example : 1234.12345
    1234.
    .1234
    0.0
    1.0
    1.00
    1.000

    4 last invalid number don't validate by my regex
    how I can fix my regular expression that (0.0 , 1.0, ..) can be invalid ?

    sorry for may bad English

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: problem in validation fix number and decimal numbers

    I don't think you can. The problem is that you're treating these numbers as text so you can't apply any kind of mathematical logic.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem in validation fix number and decimal numbers

    hi
    if found this regular expression :
    this is work fine
    Code:
    ^-?(([1-9]\d*)|0)(\.0*[1-9](0*[0-9])*)?$
    but two limitation I want
    before decimal point (.) I want only 15 numbers (length), and after it , 3 number as following sample
    Code:
    123456789012345.123
    please help me

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem in validation fix number and decimal numbers

    hi
    if found this regular expression :
    this is work fine
    Code:
    ^-?(([1-9]\d*)|0)(\.0*[1-9](0*[0-9])*)?$
    but two limitation I want
    before decimal point (.) I want only 15 numbers (length), and after it , 3 number as following sample
    Code:
    123456789012345.123
    please help me

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2004
    Posts
    97

    Re: problem in validation fix number and decimal numbers

    I found solution myself

    Code:
    ^-?(([1-9])([0-9]{1,14})?|0)(\.[0-9]?[0-9]?[1-9])?$

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: problem in validation fix number and decimal numbers

    If these are just real numbers, then you would probably be better off not using RegEx at all. RegEx is slow, as are all string manipulations. It's a very convenient tool for validating text, but when the text is just, and always, a number, there may be better alternatives. For example:

    1) Use Decimal.TryParse to convert it to a Decimal. If this fails, then the string wasn't a number anyways, and will fail your RegEx, too.

    2) Once you have the decimal you can see whether or not it is smaller than your maximum number, which is 1x10^16. If it is greater than or equal to that, it will fail your RegEx because it will be too large.

    3) If both of those tests pass then you are pretty close, so a final test might be better performed on the string:

    If originalString.IndexOf("."c) < originalString.Length-4

    The whole thing might look like this:

    Code:
    Dim dc As Decimal
    
    If Decimal.TryParse(originalString,dc) Then
     If dc < 1000000000000000D AndAlso originalString.IndexOf("."c) < originalString.Length-4 Then
      'It passed.
     Else
      'Fail
    Else
     'Fail
    End If
    I may have the decimal point off a little in there, but that would be close. It wouldn't be quite as compact as the RegEx solution, but it would most likely be faster. It would probably be faster still if you didn't use the IndexOf solution, and there are a couple ways to avoid that.

    On the other hand, if you have the RegEx solution, it probably won't make any difference which you use. Also, if the number is part of a larger string, then what I showed wouldn't work anyways.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width