To read the article online, visit http://www.4GuysFromRolla.com/webtech/090799-1.shtml

isAlpha for ASP


For those C programmers out there, you know what I'm talking about! For those who don't, there is a standard C function called isAlpha that is used to verify that a string has only alphabetic characters (A-Z, a-z). There are times when you need to verify that a user has entered only alpha characters into a string. For example, if you are asking for a unique username, you might want to limit the entry to only alpha characters (i.e. no numbers, no special characters, no spaces.)

Well, this isn't too difficult to do using ASP. We just need to create a function that takes as its input a string, and returns true if the string contains just alpha characters, and false otherwise. So let's look at some code!

Function isAlpha(str) Dim iPos, bolValid iPos = 1 bolValid = True While iPos <= Len(str) and bolValid if Asc(UCase(Mid(str,iPos,1))) < Asc("A") or _ Asc(UCase(Mid(str,iPos,1))) > Asc("Z") then _ bolValid = False iPos = iPos + 1 Wend isAlpha = bolValid End Function

This function loops through the input string one character at a time. If the current character it's examining isn't A-Z, it returns false. If we loop through the entire string and all characters are between A to Z, then we return true.

We could make use of this function by a few simple lines of ASP code. Imagine that we have a form that calls this ASP page when submitted. In that form, there was a field called UserName, which we want to verify has only alpha characters. Here's some ASP code we could use to ensure UserName was valid:

'Read in the username from our form Dim strUserName strUserName = Request("UserName") 'Make sure strUserName contains only alpha characters if isAlpha(strUserName) then 'Do processing... else Response.Write "You entered an invalid Username!" end if

This simple example demonstrates how to use the isAlpha function.

I hope you find this script useful! As always, if you have any comments, ideas, or criticisms, I encourage you to send me feedback!

Happy Programming!


Attachments:

  • Download the source for isAlpha in text format


    Related Articles

  • Technical definition for the Asc function
  • Technical definition for the Mid function
  • Technical definition for the UCase function


  • Article Information
    Article Title: isAlpha for ASP
    Article Author: Scott Mitchell
    Published Date: Tuesday, September 07, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/090799-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers