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 Numeric Characters


Question:

Is there a function that tests a variable to see if it only contains Alpha Numeric Characters (a-z, A-Z, 0-9)? I want it to return false if there are ANY numbers, spaces or special characters.

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/ischaralphanumeric.asp?frame=true
Article ID:   W16260
File Created: 2006:10:13:09:31:48
Last Updated: 2006:10:13:09:31:48