WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Validation UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Check if String Contains Only Alpha Characters


Question:

Is there a function that tests a variable to see if it only contains Alpha Characters (a-z, A-Z)? I want it to return false if there are ANY numbers, spaces or special characters. I know I could do this with some of the StrIndex functions but like i said, i am feeling lazy this morning

Answer:

; Checks for a-z, A-Z, 0-9
#DefineFunction isAlphaNumeric(str)
   hUser32 = DllLoad(StrCat(DirWindows(1),"USER32.DLL"))

   ; Convert to UNICODE.
   sUnicode = ChrStringToUnicode(str)
   For x = 1 To StrLen(sUnicode)
      item =  Char2Num(StrSub(sUnicode,x,1))
      If !DllCall(hUser32,long:"IsCharAlphaNumericW",word:item)
         DllFree(hUser32)
         Return @FALSE
      EndIf
   Next

   DllFree(hUser32)
   Return @TRUE
#EndFunction

; Checks for a-z, A-Z
#DefineFunction isAlpha(str)
   hUser32 = DllLoad(StrCat(DirWindows(1),"USER32.DLL"))

   ; Convert to UNICODE.
   sUnicode = ChrStringToUnicode(str)
   For x = 1 To StrLen(sUnicode)
      item =  Char2Num(StrSub(sUnicode,x,1))
      If !DllCall(hUser32,long:"IsCharAlphaW",word:item)
         DllFree(hUser32)
         Return @FALSE
      EndIf
   Next

   DllFree(hUser32)
   Return @TRUE
#EndFunction
For more info see: http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/resources/strings/stringreference/stringfunctions/ischaralpha.asp?frame=true
Article ID:   W16259
File Created: 2006:10:13:09:31:22
Last Updated: 2006:10:13:09:31:22