varsha shukla
varsha shukla

Reputation: 1

how can i put check which can take first letter and rest number in textbox

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?

Upvotes: 0

Views: 332

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

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

Upvotes: 0

K.Madden
K.Madden

Reputation: 210

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

Upvotes: 1

Related Questions