954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to validate string format vb.net

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

jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
 

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.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 
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?

jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
 

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.

ChrisPadgham
Posting Pro in Training
413 posts since Sep 2009
Reputation Points: 102
Solved Threads: 78
 

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.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

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?

jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
 
Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

is it not simpler to us the isnumeric function

ChrisPadgham
Posting Pro in Training
413 posts since Sep 2009
Reputation Points: 102
Solved Threads: 78
 

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

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 
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..

jbutardo
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 8
Solved Threads: 1
 
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
Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: