|
Introduction
Many problems plague the programmer writing ASP scripts where any user, with any level of experience, can freely enter anything into an HTML text box. To further complicate the problem, the destination database may have strict requirements on the format of the data gathered from the form. The following string functions can be used to either check data entered through an HTML form and prompt the user to re-enter information if it doesn't meet your standards, or to clean up the data before it is entered into a database
IsEmail
If you have ever managed data where people can freely enter whatever their conception of an E-mail address may be, you know that their conception is quite often wrong. People will give you URLs, E-mails without the appropriate .com or other appendage, their network user name, and even a street address. IsEmail will perform several tests to verify that a user has entered a valid E-mail address.
Usage:
IsEmail("jason@vpico") 'Returns False
IsEmail("jason.stehle@vpico") 'Returns False
IsEmail("www.vpico.com") 'Returns False
IsEmail("jason@vpico@com") 'Returns False
IsEmail("jason@.vpico.com") 'Returns False
IsEmail("jason@vpico.c") 'Returns False
IsEmail("jason@vpico.com") 'Returns True
Function IsEmail(sCheckEmail)
Dim sEmail, nAtLoc
IsEmail = True
sEmail = Trim(sCheckEmail)
nAtLoc = InStr(1, sEmail, "@") 'Location of "@"
If Not (nAtLoc > 1 And (InStrRev(sEmail, ".") > nAtLoc + 1)) Then
'"@" must exist, and last "." in string must follow the "@"
IsEmail = False
ElseIf InStr(nAtLoc + 1, sEmail, "@") > nAtLoc Then
'String can't have more than one "@"
IsEmail = False
ElseIf Mid(sEmail, nAtLoc + 1, 1) = "." Then
'String can't have "." immediately following "@"
IsEmail = False
ElseIf InStr(1, Right(sEmail, 2), ".") > 0 Then
'String must have at least a two-character top-level domain.
IsEmail = False
End If
End Function
IsAlphaNumeric
Say that you need to receive data from an HTML form. The end user can enter whatever he wants into your form fields, but the database you are working with requires that no punctuation or symbols be contained in certain fields. IsAlphaNumeric will return false if sString contains anything other than letters, numbers, or spaces. You can use this function to force the user to enter data in the valid format. If you simply want to clean it yourself, progress ahead and read the StripSymbols function in the next section.
Usage:
IsAlphaNumeric("101 E. Morris St. #4045") 'Returns False
IsAlphaNumeric("101 E Morris St 4045") 'Returns True
IsAlphaNumeric("350 N. Thomas Ave.") 'Returns False
IsAlphaNumeric("350 N Thomas Ave") 'Returns True
Function IsAlphaNumeric(sString)
Dim nChar, i
IsAlphaNumeric = True
For i = 1 To Len(sString)
nChar = Asc(LCase(Mid(sString, i, 1)))
If not ((nChar > 47 And nChar < 58) or_
(nChar > 96 And nChar < 123) or_
nChar = 32) Then
IsAlphaNumeric = False
Exit For
End If
Next
End Function
StripSymbols
If you want to ensure clean data without prompting the user to make changes, use StripSymbols instead of IsAlphaNumeric. StripSymbols can be used to remove any nonalphanumeric data from the string before it is written to the database.
Usage:
StripSymbols("350 N. Thomas Ave.") 'Returns "350 N Thomas Ave"
StripSymbols("101 E. Morris St. #4045") 'Returns "1015 E Morris St 4045"
Function StripSymbols(sString)
Dim nCharPos, sOut, nChar
nCharPos = 1
sOut = ""
For nCharPos = 1 To Len(sString)
nChar = Asc(Lcase(Mid(sString, nCharPos, 1)))
If ((nChar > 47 And nChar < 58) or_
(nChar > 96 And nChar < 123) or_
nChar = 32) Then
sOut = sOut & Mid(sString, nCharPos, 1)
End If
Next
StripSymbols = sOut
End Function
IsValidLoginFormat
If you require that visitors must register to use a part of your site, and you allow them to choose their own login name or password, then IsValidLoginFormat is for you. This function ensures that the user chooses a login name or password that fits your guidelines for acceptable length, and that it consists only of letters and numbers. I make sure that this function returns true before checking a user database to make sure the login isn't already taken.
Usage:
IsValidLoginFormat("johndoe3") 'Returns True
IsValidLoginFormat("john doe") 'Returns False, has a space
IsValidLoginFormat("john") 'Returns False, too short
IsValidLoginFormat("johnwilliamdoe") 'Returns False, too long
Function IsValidLoginFormat(sString)
Dim nChar, i
IsValidLoginFormat = True
'Login must be between 8 and 12 characters long. Adjust to your needs.
If (Len(sString) >= 8) And (Len(sString) <= 12) Then
For i = 1 To Len(sString)
nChar = Asc(LCase(Mid(sString, i, 1)))
If not ((nChar > 47 And nChar < 58) or_
(nChar > 96 And nChar < 123)) Then
IsValidLoginFormat = False
Exit For
End If
Next
Else
IsValidLoginFormat = False
End If
End Function
StripNonNumeric
Here's one for cleaning up phone numbers, when your database administrator requires that no hyphens, parentheses, or any other symbols are included in phone or fax numbers.
Usage:
StripNonNumeric("(602) 757-2092") 'Returns "6027572092"
StripNonNumeric("602-757-2092") 'Returns "6027572092"
StripNonNumeric("602.757.2092") 'Returns "6027572092"
Function StripNonNumeric(sString)
Dim i, sChar, sNonNumeric
sNonNumeric = ""
For i = 1 To Len(sString)
sChar = Mid(sString, i, 1)
If Asc(sChar) > 47 And Asc(sChar) < 58 Then
sNonNumeric = sNonNumeric & sChar
End If
Next
StripNonNumeric = sNonNumeric
End Function
Data Output String Functions
Sometimes data in its original form may not be desirable for output. The XDigits and PadString functions are useful when a character string or number must be represented at a specific length. RemoveHTML tags allows you to convert a string containing HTML tags into a string suitable for a text file or E-mail message.
Xdigits Function
Say you need to have a count in a dynamically generated table that must have 3 digits, or a value for the day of the month that must be displayed as two digits. XDigits can format an integer or string of integers to have as many zeros to the left of it to fit a specified length. The parameters for XDigits are: sNumberString, the integer or string integers that need to be formatted; and nReturnLength, the length of the string returned by the function. If nReturnLength is less than the length of sNumberString, XDigits will return the rightmost digits in sNumberString fitting nReturnLength.
Usage:
XDigits("24",4) 'Returns "0024"
XDigits(58,3) 'Returns "058"
XDigits("1999",2) 'Returns "99"
Function XDigits(sNumberString, nReturnLength)
If nReturnLength > Len(sNumberString) Then
XDigits = String(nReturnLength - Len(sNumberString), "0") & sNumberString
Else
XDigits = Right(sNumberString, nReturnLength)
End If
End Function
PadString Function
If you have to create a data report in a space-delimited format either for an ASCII file or an HTML page inside <pre> tags, PadString is your savior. PadString is a multipurpose function similar to XDigits, except it is used for formatting character strings. PadString adds characters to or truncates a string to fit a specified length. The parameters for PadString are: sString, the string to be padded or truncated; nReturnLength, the length of the string returned by the function; sPadCharacter, the character used to pad the string if the string is shorter than nReturnLength; and nSide, which indicates where the padding should occur. nSide has 3 values: 0, align the string to the left; 1, align the string to the right; and 2, center align the string. sPadCharacter can be any character, including spaces, hyphens, and underscores.
Usage:
PadString("Hello world.", 5, "_", 0) 'Returns "Hello"
PadString("Hello", 8, "_", 0) 'Returns "Hello___"
PadString("Hello", 6, " ", 1) 'Returns " Hello"
PadString("Hello", 9, "!", 2) 'Returns "!!Hello!!"
Function PadString(sString, nReturnLength, sPadCharacter, nAlign)
If nReturnLength > Len(sString) Then
Select Case nAlign
Case 0 'Align to the left
PadString = sString & String(nReturnLength - Len(sString), sPadCharacter)
Case 1 ' Align to the right
PadString = String(nReturnLength - Len(sString), sPadCharacter) &_
sString
Case 2 'Center align
Dim nDivide1, nDivide2, nDivideNumber
nDivideNumber = nReturnLength - Len(sString)
nDivide1 = Int(nDivideNumber / 2)
nDivide2 = nDivideNumber - nDivide1
PadString = String(nDivide1, sPadCharacter) &_
sString &_
String(nDivide2, sPadCharacter)
End Select
Else
PadString = Left(sString, nReturnLength)
End If
End Function
RemoveHTMLTags
RemoveHTMLTags is a function that removes angle brackets and any characters between them. This function can be used in a program where a generated string will be returned to the user as HTML in a Web page and as plain text in an E-mail message. Note that this program does not decode any HTML character encoding, such as """ for quotation marks, or & for an ampersand.
Usage:
RemoveHTMLTags("<p>Company Name: <BR>") 'Returns "Company Name: "
RemoveHTMLTags("<p>Your user name is <B>TOM4318.</b></p>") 'Returns "Your user name is TOM4318."
Function RemoveHTMLTags(sIn)
Dim nCharPos, sOut, bInTag, sChar
sOut = ""
bInTag = False
For nCharPos = 1 To Len(sIn)
sChar = Mid(sIn, nCharPos, 1)
If sChar = "<" Then
bInTag = True
End If
If Not bInTag Then sOut = sOut & sChar
If sChar = ">" Then
bInTag = False
End If
Next
RemoveHTMLTags = sOut
End Function
Conclusion
String functions are our friends. They help relieve us of the heartbreak of bad data in the harsh data-entry world of the Internet. They allow our output to be pretty in a variety of circumstances. So remember your friends, and use them well. You will find they will reward you again and again.
About the Author
Jason Stehle is a VB, ASP, and FoxPro programmer for a trade magazine company in Phoenix. His current interests include linking Windows-based data systems to Web front ends and various other buzzword-related job functions. He believes that real coders use Notepad. He can be reached at jason@vpico.com.
|