I wrote this regex function
Public Function ValidateEmailAddress(ByVal txtEmailAddress As String) As Boolean
Dim pattern As String
pattern = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$"
If Regex.IsMatch(txtEmailAddress, pattern) Then
Return True
Else
Return False
End If
End Function
And on-calling the function in a sub procedure:
If ValidateEmailAddress(txtEmailAddress.Text) = True Then
Else
MessageBox.Show("Email Not Valid")
End If
Sub Procedure
When i enter the email, correctly or incorrectly i get the same message i.e email not valid,in addition to that on stepping through the code after the If statement it ignores the return true. Please can anyone spot what the issue is here?
As an alternative to BigYellowCactus's answer :- You can use something like this - I think this is preferable to using regex:
Public Shared Function IsValidEmailAddress(ByVal emailAddress As String) As Boolean
Try
Dim address As New MailAddress(emailAddress)
Return True
Catch ex As Exception
Return False
End Try
End Function
This will return False with #abc.com and bill# whereas just checking for # will return True
Related
Consider :
Private Function isAvailableQuantity() As Boolean
Try
sqL = "SELECT StocksOnHand FROM ITEM WHERE ItemNo = " & Val(txtSearch.Text) & ""
ConnDB()
cmd = New OleDbCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read = True Then
If Val(txtQuantity.Text) <= dr(0) Then
isAvailableQuantity = True
Else
MsgBox("Insuficient stocks", MsgBoxStyle.Critical, "Validate Stocks")
txtSearch.Clear()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
End Function
I don't know what to do. The older version visual studio doesn't get this error.
i am using vs 2022 right now and it seems to have an error on the contrary vs 2010 doesn't
In VB.NET, a Function is a method that returns a value and a Sub is a method that doesn't return a value. If your method doesn't need to return anything, use a Sub, e.g.
Private Sub DoSomething()
'Do some stuff here.
End Sub
If you do use a Function then there are two ways to return a value. The bad way is to assign a value to the implicit local variable that is named after the method, e.g.
Private Function DoSomething() As Boolean
'Do some stuff here.
DoSomething = True
End Function
That way basically only exists to support upgraded VB6 code. If your teacher is showing you that then they are obviously an old VB6 developer who hasn't actually learned VB.NET properly. The good way to return a value is with an explicit Return statement, e.g.
Private Function DoSomething() As Boolean
'Do some stuff here.
Return True
End Function
If there are multiple paths that execution could take through your code, you need to make sure that a value is returned on all of them. As an example, this code does not do that:
Private Function DoSomething() As Boolean
If Date.Now.DayOfWeek = DayOfWeek.Monday Then
Return True
End If
End Function
That will return True on Mondays but it doesn't return anything on other days. In this specific case, one fix would be this:
Private Function DoSomething() As Boolean
If Date.Now.DayOfWeek = DayOfWeek.Monday Then
Return True
End If
Return False
End Function
If you are going to use the bad way to return a value and you want to return a particular value on all but one code path then the logical thing to do is to set the return value at the start to the default and then only change it in that one place, e.g.
Private Function DoSomething() As Boolean
DoSomething = False
If Date.Now.DayOfWeek = DayOfWeek.Monday Then
DoSomething = True
End If
End Function
That last example is the easiest (although not best) fix for your scenario.
Following method, I am using to validate the Base64
Public Function ValidateBase64String(ByVal sString As String) As Boolean
If (sString.Length <> 4) Then
Dim b As Byte() = Convert.FromBase64String(sString)
Return True
Else
Return False
End If
End Function
I am passing "johnjohn" as a string to the method and it is returning that following string is the base64 string. What is the reason that instead of returning false it returns true.
Following method works almost every time
Console.WriteLine(ValidateBase64String("johnjohn"))-> it will always returns false
Public Function ValidateBase64String(ByVal value As string) As Boolean
Try
If value.Length <> 4 AndAlso (Base64Decode(value) IsNot Nothing AndAlso System.Text.RegularExpressions.Regex.IsMatch(Base64Decode(value), "^[a-zA-Z0-9\+/]*={0,3}$")) Then
Return True
End If
Catch ex As Exception
End Try
Return False
End Function
Public Function Base64Decode(ByVal base64EncodedData As String) As String
Try
Dim base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData)
Return System.Text.Encoding.UTF8.GetString(base64EncodedBytes)
Catch ex As Exception
End Try
Return Nothing
End Function
I want to validate only numbers. I used below cord.But it doesn't work.
Public Function validateNumber(number) As Boolean
Dim num As New Regex("^[0-9]{9}$")
If num.IsMatch(number) Then
Return True
Else
Return False
End If
End Function
Try below regex...
Dim num As New System.Text.RegularExpressions.Regex("^\d+$")
Return num.Match(number).Success
you can use ISnumeric(yourno) it will return true if it is a number , you can use bellow link for more info https://msdn.microsoft.com/en-us/library/6cd3f6w1(v=vs.90).aspx
I know the statement String.IsNullOrEmpty which I use regulary.
But now I'm wondering if I could use something similar for Dictionaries
My Current Code:
Public Property ExpectedResults As Generic.Dictionary(Of String, String)
Public Function SomeMethod() As Boolean
'Some code
If (IsNothing(ExpectedResults)) Then
Return True
End If
If ExpectedResults.Count = 0 Then
Return True
End If
For Each key As String In ExpectedResults.Keys
'If Regex Matches value
Return True
'End If
Next
Return False
End Sub
Is it possible to replace those 2 Ifs with 1 single statement? Or am I searching for something that doesn't exist?
Thank you for your time.
If I understand correctly, You want to check if your Dictionary is NOT nothing and also contains items? In this case I would go with
If Not IsNothing(ExpectedResults) AndAlso ExpectedResults.Count > 0 Then
Return True
End If
The null-conditional operator makes it pretty tidy:
If ExpectedResults?.Count > 0 Then
Return True
End If
I am trying to validate email addresses to my vb.net windows form application,this is the validate email function written in another class
Public Function ValidateEmailAddress(strEmailAddress As String) As Boolean
Dim pattern As String
pattern = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
If Regex.IsMatch(strEmailAddress, pattern) Then
Return True
Else
Return False
End If
End Function
I then called the IsValid function from another class by writing this code
If ValidateEmailAddress(txtEmailAddress.Text) Then
MessageBox.Show("Email Valid")
Else
MessageBox.Show("Email Not Valid")
End If
On-Stepping throught this code,it skips past the Return True statement in the ValidateEmailAddress function an also skips the MessageBox.Show("Email Valid") in the isValid() function. therefore i get an Email not Valid return statement even wwhen i enter the correct email format. Please is the regex pattern correct or can anyone spot anything im doing wrong here. Thanks
For me it looks like a Regex problem. Try this one:
Public Function ValidateEmailAddress(strEmailAddress As String) As Boolean
Dim EmailRegex As New Regex("^([a-zA-Z0-9_\-\.\']+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
If EmailRegex.IsMatch(strEmailAddress) Then
Return True
Else
Return False
End If
End Function
Sorry for brief answer, at the moment I have very little time to point out where the error may be.