3

how can i check if a string only contains 4 digit numbers ( or a year )
i tried this

 Dim rgx As New Regex("^/d{4}")      
    Dim number As String = "0000"
    Console.WriteLine(rgx.IsMatch(number)) // true
    number = "000a"
    Console.WriteLine(rgx.IsMatch(number)) // false
    number = "000"
    Console.WriteLine(rgx.IsMatch(number)) //false
    number = "00000"
    Console.WriteLine(rgx.IsMatch(number))  // true <<< :(

this returns false when its less than 4 or at characters but not at more than 4

thanks!

user2653652
  • 57
  • 1
  • 10

3 Answers3

5

I actually wouldn't use a regex for this. The expression is deceptively simple (^\d{4}$), until you realize that you also need to evaluate that numeric value to determine a valid year range... unless you want years like 0013 or 9015. You're most likely going to want the value as an integer in the end, anyway. Given that, the best validation is probably just to actually try to convert it to an integer right off the bat:

Dim numbers() As String = {"0000", "000a", "000", "00000"}
For Each number As String In numbers
    Dim n As Integer
    If Integer.TryParse(number, n) AndAlso number.Length = 4 Then
        'It's a number. Now look at other criteria

    End If 
Next 
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Ironic, `Integer.TryParse` is exactly the same as `IsNumeric`, just with additional verification. – AStopher Mar 28 '15 at 14:58
  • the difference is i think that it parses it to a integer that cant have decimal numbers. and thats why it works for me i think – user2653652 Mar 28 '15 at 15:01
  • 2
    TryParse() is **NOT** the same as IsNumeric(). It actually produces an integer variable in the result. IsNumeric() uses a different implementation for compatibility with old code, and it's a naive and stupid function. – Joel Coehoorn Mar 28 '15 at 16:19
  • 1
    @user2653652 It's not just that it allows decimals. it's also that it deliberately preserves some ... odd behaviors for backwards compatibility purposes with code migrated from the vb6 era. – Joel Coehoorn Mar 28 '15 at 16:28
0

Use LINQ to check if All characters IsDigit:

Dim result As Boolean = ((Not number Is Nothing) AndAlso ((number.Length = 4) AndAlso number.All(Function(c) Char.IsDigit(c))))
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
  • There's no error handling here, there are *hundreds* of situations where it could cause problems. – AStopher Mar 28 '15 at 14:25
  • 1
    Error handling?? The number variable is defined and initialized as `Dim number As String = "0000"`. How can there be *"hundreds of situations where it could cause problems"*? – Bjørn-Roger Kringsjå Mar 28 '15 at 14:33
-1

You should use the .NET string manipulation functions.

Firstly the requirements, the string must:

  • Contain exactly four characters, no more, no less;
  • Must consist of a numeric value

However your aim is to validate a Date:

Function isKnownGoodDate(ByVal input As String) As Boolean 'Define the function and its return value.
    Try 'Try..Catch statement (error handling). Means an input with a space (for example ` ` won't cause a crash)
        If (IsNumeric(input)) Then 'Checks if the input is a number
            If (input.Length = 4) Then
                Dim MyDate As String = "#01/01/" + input + "#"
                If (IsDate(MyDate)) Then
                    Return True
                End If
            End If
        End If
    Catch
        Return False
    End Try
End Function

You may experience a warning:

Function isKnownGoodDate does not return a value on all code paths. Are you missing a Return statement?

this can be safely ignored.

AStopher
  • 4,207
  • 11
  • 50
  • 75
  • Thanks, just to be curious, why shoudnt i use Regex, something about performance? – user2653652 Mar 28 '15 at 14:23
  • and doesnt IsNumeric also return true on double? – user2653652 Mar 28 '15 at 14:27
  • 1
    IsNumeric() is a very naive evaluation. Not everything that evaluates as numeric can be converted to a number, and not everything that's a number evaluates as numeric. – Joel Coehoorn Mar 28 '15 at 14:33
  • i tested the code input = "00.1" also returns as true, and return false doesnt happen – user2653652 Mar 28 '15 at 14:39
  • @user2653652 Check the code I added, that should do the job for you (as I tested it myself), it's more likely to work for you since `Date` types cannot be `01/01/19.04`. – AStopher Mar 28 '15 at 14:42
  • @user2653652 Just curious, are you attempting to validate dates with certain properties (e.g, dates which are in the future, in the past 100 years, etc)? – AStopher Mar 28 '15 at 14:49