Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Check string entered is valid month

Hello,
Hello in vb.net how can I check if the string entered is the valid month and return the value of the month.

Eg:
Str = Jan

Result: Jan

Str= Fb
Result: Not valid month

Thanks
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Well its up to you to decide which is the best way
You could have an array , a text file , a database table
Each time you get a value you should search if it is matched and accordingly act.
e.g
Fb ...is it February or the user wanted to check news on Facebook and the screen refreshed and took him/her to the application
to be 100% sure just dump the string input and limit the user to a combobox...no need for extra validation ,searching..whatever..the values will always be 12,consistent and valid.
try a function like this:

Function validateMonth(v As String) As String
        Dim DateValue As Date
        If (Date.TryParse("1 " + v + " 2000", DateValue)) Then
            Return v
        Else
            Return "Not valid month"
        End If
    End Function

Open in new window

then:

Dim Str As String = "Jan"
        MessageBox.Show(validateMonth(Str))

        Str = "Fb"
        MessageBox.Show(validateMonth(Str))

Open in new window

try customize accordingly, for example, return a true/ false from function above instead, etc.
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Would it not be better the user has to select a month from eg. a combobox rather than type a month in:It is good idea but, the user wants to type.

Thanks Andy
 
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

haha, great! Perfect! Thanks a lot!
Avatar of RIAS

ASKER

Thanks all!