Hi,

I just want to know how can vb.net validate the format of a certain string
for example, i have a string with 2011/13/02, and I want to check if it's format is in yyyy-dd-MM format,. how can this be done?

Thanks

Recommended Answers

All 10 Replies

The easiest way would be to split it into three strings as mystring.Split("\") then validate each field separately. A more complicated way would be to use a regular expression.

The easiest way would be to split it into three strings as mystring.Split("\") then validate each field separately. A more complicated way would be to use a regular expression.

can you teach me on how regex works?

I know this is not what you asked but if you just want to check if it is a valid date then you can use the isdate() function.

Well don't I just feel like a complete moron? isdate() (DOH!).

As for regex, I use them so infrequently that when I need a particular one I can usually just find it online or in a book. Wrox Press Beginning Regular Expressions by Andrew Watt is excellent. When you discover that this "intro" book is almost 800 pages you'll get the idea that regular expressions are non-trivial.

Well don't I just feel like a complete moron? isdate() (DOH!).

As for regex, I use them so infrequently that when I need a particular one I can usually just find it online or in a book. Wrox Press Beginning Regular Expressions by Andrew Watt is excellent. When you discover that this "intro" book is almost 800 pages you'll get the idea that regular expressions are non-trivial.

okay, i found a solution on the date format, while i'm having trouble on validating if the string is numeric in regex.. i tried the Regex(“\d+”), but it didn't work ..

how can i validate it using regex?

is it not simpler to us the isnumeric function

It would be easier to use isnumeric or isdate but he asked about regex so I thought I'd answer.

is it not simpler to us the isnumeric function

okay, but the thing is, I want to use the regex for the validation procedure..
thanks by the way for the comments..

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub btnValidate_Click(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click

        Dim pattern As String = "^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$"
        Dim regex As New Regex(pattern)

        If regex.IsMatch(txtInput.Text) Then
            MsgBox("is valid")
        Else
            MsgBox("is not valid")
        End If

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.