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
Related
I have one vb.net code where the ID text box is checking if order ID is numeric only. Now my boss want me to change this with first letter and rest numbers like(A0000000) so he needs both ways full numeric and first letter and numeric.
my existing code is.
ElseIf (Not IsNumeric(txtworkorderID.Text)) Then
invalidWorkOrderNumber = True
how can i change this to check if all are numeric or alpha numeric?
I have little bit of programming knowledge.Can someone please help me?
You could use a function like if this is what you mean? Sorry bit trouble understanding what you fully want.
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
If Not Char.IsLetter(StringToCheck.Chars(0)) Then
Return False 'first character is not a letter
End If
'other check if number
For i = 1 To StringToCheck.Length - 1
If Char.IsLetter(StringToCheck.Chars(0)) Then
Return False 'characters contain a letter
End If
next
Return True 'Return true if first character is a letter and rest number
End Function
Basically this will look at the string check to see if the first character is a letter, if it isn't return false, it will then check every character after the first to make sure its not a letter.
If Regex.IsMatch(number, "^[0-9 ]+$") Then
'This will see if the whole string is all numbers! so maybe mix both this and the on above to confirm either ways acceptable?
End If
You could do this with String.Substring to pull out each part and test it both ways. For instance, something like this:
Public Function ParseId(id As String) As ParsedId
Dim number As Integer
If id?.Length > 0 Then
If Integer.TryParse(id, number) Then
Return New ParsedId() With {
.Number = number }
End If
End If
If id?.Length > 1 Then
If Char.IsLetter(id(0)) And Integer.TryParse(id.Substring(1), number) Then
Return New ParsedId() With {
.Letter = id(0),
.Number = number }
End If
End If
Return Nothing
End Function
Public Class ParsedId
Public Property Letter As String
Public Property Number As Integer
End Class
But, if there's any chance that you are going to need to make it even more flexible in the future, you may want to consider using regex (which is shorter anyway):
Public Function ParseId(id As String) As ParsedId
Dim m As Match = Regex.Match(id, "^(?<Letter>[A-Z])?(?<Number>\d+)$")
If m.Success Then
Return New ParsedId With {
.Letter = m.Groups("Letter").Value,
.Number = Integer.Parse(m.Groups("Number").Value) }
Else
Return Nothing
End If
End Function
I have a variable to hold an id
Dim empId as String
So the format for a valid ID is:
'the first character should be a letter [A-Z]
'the rest of the string are digits e.g M2895 would be a valid id
I would like to check each of those characters to see if they fit the correct ID
So far, I have come across the isNumeric() function. Is there a similar function in VB.NET to check if a character is a string or alpha character?
You can use RegularExpressions instead of checking each character of your string by hand:
Dim empId as String = "M2895"
If Regex.IsMatch(empId, "^[A-Z]{1}[0-9]+$") Then
Console.WriteLine("Is valid ID")
End If
If you need a function isAlpha you can create this function by using RegularExpressions too:
Private Function isAlpha(ByVal letterChar As String) As Boolean
Return Regex.IsMatch(letterChar, "^[A-Z]{1}$")
End Function
For completion, to support estonian alphabet too, you can use the following:
Dim empId as String = "Š2859"
If Regex.IsMatch(empId, "^[^\W\d_]{1}[0-9]+$") Then
Console.WriteLine("Is valid ID")
End If
You can use functions which works for all Unicode characters
Char.IsLetter Method (String, Int32)
Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.
Char.IsDigit Method (Char)
Indicates whether the specified Unicode character is categorized as a decimal digit.
So you end up with validation like
Public Function IsValid(id As String)
If Char.IsLetter(id, 0) = False Then
Return False
End If
If id.Skip(1).All(Char.IsDigit) = False Then
Return False
End If
Return True
End Function
Here is my take an isAlpha function, answering the title of this question:
Public Shared Function isAlpha(ByVal s as String) as Boolean
if s is Nothing then return False
For Each c As Char in s
If not Char.IsLetter(c) then return False
Next
return True
End Function
However, to answer the body of the question, here is my mod of #Fabio's answer:
Public Function isMyAlphaCode(id As String) as Boolean
if id is Nothing then return False
if id.length < 2 then return False
If Char.IsLetter(id, 0) Then
Return False
End If
If id.Skip(1).All(Char.IsDigit) Then
Return False
End If
Return True
End Function
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 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
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.