Validation methods syntex in VB

date, email and numeric validation methods. String can be of any type it depends upon requirement. The method I have created for string validation will return false if contains any special character.

‘Date Validation Method
Function ifThisIsDate( ByVal givenDate As String, ByVal givenFormat As String) As Date

Dim dateString, format As String
Dim result As Date
result = Date .Now
Dim provider As CultureInfo = CultureInfo.InvariantCulture
dateString = givenDate
format = givenFormat
provider = New CultureInfo( “fr-FR”)
Try
result = Date .ParseExact(dateString, format, provider)
Catch ex As FormatException
result = Date .Parse( “1/1/1900″)
End Try

Return result

End Function

Example :
ifThisIsDate(” 23/3/2000″, “d/M/yyyy” ).ToString( “d/M/yyyy” ) ‘return = 23/3/2000
ifThisIsDate(” 23/23/2000 “, “d/M/yyyy” ).ToString( “d/M/yyyy” ) ‘return = 1/1/1900
ifThisIsDate(” 3/23/2000 “, “d/M/yyyy” ).ToString( “d/M/yyyy” ) ‘return = 1/1/1900

‘Email Address validation Method
Function ValidateEmailAddress( ByVal emailAddress As String) As Boolean

Dim flag As Boolean

Dim pattern As String = “^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$”

Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)

If emailAddressMatch.Success Then
flag = True
Else
flag = False
End If

Return flag

End Function
Example :
ValidateEmailAddress(tehmina@cyber-gear.com) ‘ return true
ValidateEmailAddress(tehmina.qureshi@cyber-gear.com) ‘ return true
ValidateEmailAddress(tehmina.qureshi) ‘ return false

‘String Validation Method
Function ValidateString( ByVal stringToValidate As String) As Boolean

Dim flag As Boolean

Dim pattern As String = “^(\w(\s)?)+$”

Dim stringMatch As Match = Regex.Match(stringToValidate, pattern)

If stringMatch.Success Then
flag = True
Else
flag = False
End If

Return flag

End Function
Example :
ValidateString(tehmina qureshi) ‘ return true
ValidateString(tehmina’\\) ‘ return false
ValidateString(tehmina qureshi) ‘ return true

‘ Numeric Validation Method
Function ValidateNumeric( ByVal stringToValidate As String) As Boolean

Dim flag As Boolean

Dim pattern As String = “^[0-9]*(\.)?[0-9]+$”

Dim stringMatch As Match = Regex.Match(stringToValidate, pattern)

If stringMatch.Success Then
flag = True
Else
flag = False
End If

Return flag

End Function
Example :
ValidateNumeric(23) ‘ return true
ValidateNumeric(23s) ‘ return false
ValidateNumeric(23231.4) ‘ return true

Advertisements

~ by UTS on April 11, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: