When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Tuesday, September 07, 1999

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.)

- continued -

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!


  • 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

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article