QTP Tip: Check if the given string is AlphaNumeric

799 views
Skip to first unread message

Tirupati Jummidi

unread,
Nov 10, 2010, 2:06:17 PM11/10/10
to QTP - HP Quick Test Professional - Automated Software Testing
Hi,

I have come across with a scenario where I have to check if the text
is alpha numeric that is displayed in the application.

For the above scenario, with some work around, I got the solution for
it.

Here, I am coming up with two possible solutions, of course, there may
be many different ways to do it.

Enjoy with the below function whenever you come across with this kind
of scenario.

' Check if the given string is AlphaNumeric

' 1st Approach:
Function IsAlphaNumeric(strToCheck)
Dim regEx 'Create variable.
Set regEx = New RegExp ' Create a regular expression.
strToCheck = Replace(strToCheck," ","") 'Replace Spaces in the
strToCheck, if any
strToCheck = Replace(strToCheck,vbTab,"") 'Replace Tab Spaces
in the strToCheck, if any
strToCheck = Replace(strToCheck,vbNewLine,"") 'Replace Line
Spaces in the strToCheck, if any
regEx.Pattern = "^\w*$" 'This matches letters, numbers, and
underscores
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
IsAlphaNumeric = regEx.Test(strToCheck)
Set regEx = Nothing
End Function


'2nd Approach
Function checkAlphaNumeric(strInputText)
Dim intCounter
Dim strCompare
Dim strInput
Dim i
Dim arrAlNum()

ReDim arrAlNum((len(strInputText)-1))

'Assign each character of input to an array element
For i = 0 To UBound(arrAlNum)
arrAlNum(i) = Mid(strInputText, i + 1, 1)
Next

'Work through each element to check for presence of invalid
character
'(any non-alpha char)
For i = 0 To UBound(arrAlNum)
If Not ((arrAlNum(i) = Chr(32) ) Or (arrAlNum(i) > Chr(47) And
arrAlNum(i) < Chr(58)) Or (arrAlNum(i) > Chr(64) And arrAlNum(i) <
Chr(91)) Or (arrAlNum(i) > Chr(96) And arrAlNum(i) < Chr(123))) Then
checkAlphaNumeric = False
Exit Function
End If
Next
'Return True if no string is only alphanumeric
checkAlphaNumeric = True
End Function

' QTP Usage
Msgbox checkAlphaNumeric("IS1 is2 IS3 is4")
Msgbox IsAlphaNumeric("IS1 is2 IS3 is4")


--


Regards and Thanks,
Tirupati. J

"Coming together is a beginning, Keeping together is progress, Working
together is success."

YOU ARE WHAT YOU WISH TO BE.
Reply all
Reply to author
Forward
0 new messages