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: Saturday, July 31, 1999

Checking a String for Certain Characters, Part 2

  • Read Checking a String for Certain Characters, Part 1

  • Last article I showed you how to validate a string for certain inputs using client-side JavaScript. I am going to use the exact same approach for the server-side ASP script. The syntax is really the only difference here.

    - continued -

    Let me just reiterate our approach:
    Know what characters you want to include; step through their form input one character at a time and test to see if the current character is in the set of inclusive characters. If it is not, then flag that the string is invalid.

    Now, onto the code (available in text format at the end of this article...)!

    <%@ Language=VBScript %>
    <% Option Explicit %>
    <% 'Read in the username Dim strUserName strUserName = Request("UserName") 'Now, we need to validate the username. 'That is, it can only contain the following characters: ' A - Z ' a - z ' 0 - 9 ' - (dash) ' _ (underscore) 'We are going to step through the string once character at a time. Dim iStrPos, iLoop, strCurrentChar, bolValidString For iLoop = 1 to Len(strUserName) strCurrentChar = Mid(strUserName, iLoop, 1) if strCurrentChar = "_" _ ' if we're on an underscore or strCurrentChar = "-" _ ' or we're on a dash or isNumeric(strCurrentChar) _ ' or we're on a number or (Asc(UCase(strCurrentChar)) >= Asc("A") and _ Asc(UCase(strCurrentChar)) <= Asc("Z")) _ 'or we're on a number then 'then we're on a valid character, do nothing else 'but if we're not, the string is invalid! bolValid = False end if Next 'Did we find a valid string? if Not bolValid then 'Take them to a page explaining that they've entered an 'invalid username Response.Redirect "InvalidUserName.htm" end if 'Otherwise we can go ahead and do whatever processing we need to do! :) '... %>

    Pretty straight-forward. With the client-side validation, you don't need to make a round-trip to the server to validate the UserName, so there's one advantage to using client-side code.

  • Read Checking a String for Certain Characters, Part 1

  • Attachments:

  • Code for Client-side JavaScript Validation in Text Format
  • Code for Server-side ASP Validation in Text Format

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