PDA

View Full Version : Does anyone have a copy of my old functions?


whammy
04-02-2009, 08:28 AM
Hi,

I'm just wondering if someone has a copy of my old functions.asp file.

I have it on another hard drive somewhere, and I'm sure I have it on a CD somewhere as well... but if I can avoid having to install another hard drive on my new computer and someone has it handy, that would be wonderful! I've searched for it on the 'net to no avail, although I did manage to find a bunch of other old code I had.

Thanks!

whammy
04-02-2009, 08:46 AM
I found them on the Wayback Machine, after noticing a search parameter I overlooked. :)


<%
'*************************************************
' Commonly Used Functions
' Last Modified 8/8/2003
'
' Validation Functions:
' =====================
' IsAlphaNumeric()
' IsCurrency()
' IsDateFormat()
' IsDigits()
' IsEmail()
' IsPhoneNumber()
' IsPostalCode()
' IsProvince()
' IsSSN()
' IsState()
' IsURL()
' IsZipCode()
'
' String Manipulation Functions:
' ==============================
' ExtractAlphaNumeric()
' ExtractNumbers()
' ExtractWordChars()
' FormatPhoneNumber()
' FormatPostalCode()
' FormatSSN()
' FormatZipCode()
' RemoveExtraSpaces()
' RemoveLeadingZeroes()
' RemoveSpaces()
'
' Type Conversion Functions:
' ==========================
' EmptyToSpace()
' NullToEmpty()
' NullToZero()
'
' Miscellaneous Functions:
' ========================
' Comma2Pipe()
' CountWords()
' DateExt()
' Date8()
' Date8Convert()
' Date14()
' EnglishDate()
' Indent()
' IsChecked()
' IsSelected()
' MaskCCNumber()
' RequestFormat()
' SQLSafe()
' VbCrLfToBreak()
'
'*************************************************

'*************************************************
' Validation Functions
'*************************************************

Function IsAlphaNumeric(str)
Dim ianRegEx
Set ianRegEx = New RegExp
ianRegEx.Pattern = "^[a-zA-Z0-9]+$"
ianRegEx.Global = True
IsAlphaNumeric = ianRegEx.Test(str)
End Function

Function IsCurrency(str)
Dim icRegEx
Set icRegEx = New RegExp
icRegEx.Pattern = "^\$?\d+(\.\d{2})?$|^\$?\.\d{2}$"
IsCurrency = icRegEx.Test(str)
End Function

Function IsDateFormat(str)
Dim idfRegEx
Set idfRegEx = New RegExp
idfRegEx.Pattern = "^(\d{1,2})\/(\d{1,2})\/(\d{4})$"
IsDateFormat = idfRegEx.Test(str)
End Function

Function IsDigits(str)
Dim idRegEx
Set idRegEx = New RegExp
idRegEx.Pattern = "^\d+$"
IsDigits = idRegEx.Test(str)
End Function

Function IsEmail(str)
Dim ieRegEx
Set ieRegEx = New RegExp
ieRegEx.Pattern = "^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
IsEmail = ieRegEx.Test(str)
End Function

Function IsPhoneNumber(str)
Dim ipnRegEx
Set ipnRegEx = New RegExp
ipnRegEx.Pattern = "^1?[1-9]\d{9}$"
IsPhoneNumber = ipnRegEx.Test(ExtractNumbers(str))
End Function

Function IsPostalCode(str)
Dim ipcRegEx
Set ipcRegEx = New RegExp
ipcRegEx.IgnoreCase = True
ipcRegEx.Pattern = "^[a-z]\d[a-z][ .-]?\d[a-z]\d$"
IsPostalCode = ipcRegEx.Test(str)
End Function

Function IsProvince(str)
Dim ipRegEx
Set ipRegEx = New RegExp
ipRegEx.IgnoreCase = True
ipRegEx.Pattern = "^(AB|BC|MB|NB|NF|NT|NS|NU|ON|PE|QC|SK|YT)$"
IsProvince = ipRegEx.Test(str)
End Function

Function IsSSN(str)
Dim ssnRegEx
Set ssnRegEx = New RegExp
ssnRegEx.Pattern = "^\d{3}[ -]?\d{2}[ -]?\d{4}$"
IsSSN = ssnRegEx.Test(str)
End Function

Function IsState(str)
Dim isRegEx
Set isRegEx = New RegExp
isRegEx.IgnoreCase = True
isRegEx.Pattern = "^(AL|AK|AZ|AR|CA|CO|CT|DC|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NH|NJ|NM|NY|NV |NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY|AA|AP|AE|AS|FM|GU|MH|MP|PW|PR|VI)$"
IsState = isRegEx.Test(str)
End Function

Function IsURL(str)
Dim iuRegEx
Set iuRegEx = New RegExp
iuRegEx.Pattern = "(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
IsURL = iuRegEx.Test(str)
End Function

Function IsZipCode(str)
Dim izcRegEx
Set izcRegEx = New RegExp
izcRegEx.Pattern = "^(\d{5})[ .-]?(\d{4})?$"
IsZipCode = izcRegEx.Test(ExtractNumbers(str))
End Function

'*************************************************
' String Manipulation Functions
'*************************************************

Function ExtractAlphaNumeric(ByVal str)
If IsNull(str) Then Exit Function
Dim eanRegEx
Set eanRegEx = New RegExp
eanRegEx.Pattern = "[^a-zA-Z0-9]"
eanRegEx.Global = True
ExtractAlphaNumeric = eanRegEx.Replace(str,"")
End Function

Function ExtractNumbers(ByVal str)
If IsNull(str) Then Exit Function
Dim enRegEx
Set enRegEx = New RegExp
enRegEx.Pattern = "\D"
enRegEx.Global = True
ExtractNumbers = enRegEx.Replace(str,"")
End Function

Function ExtractWordChars(ByVal str)
If IsNull(str) Then Exit Function
Dim ewcRegEx
Set ewcRegEx = New RegExp
ewcRegEx.Pattern = "\W"
ewcRegEx.Global = True
ExtractWordChars = ewcRegEx.Replace(str,"")
End Function

Function FormatPhoneNumber(ByVal str)
If IsNull(str) Then Exit Function
Dim fpRegEx
Set fpRegEx = New RegExp
fpRegEx.Pattern = "^(?:1?[ .-]?)\(?([1-9]\d{2})\)?[ .-]?\s?(\d{3})[ .-]?(\d{4})$"
FormatPhoneNumber = fpRegEx.Replace(str,"($1) $2-$3")
End Function

Function FormatPostalCode(ByVal str)
If IsNull(str) Then Exit Function
Dim fpcRegEx
Set fpcRegEx = New RegExp
fpcRegEx.Pattern = "^([a-z]\d[a-z])[ .-]?(\d[a-z]\d)$"
fpcRegEx.IgnoreCase = True
FormatPostalCode = UCase(fpcRegEx.Replace(str,"$1 $2"))
End Function

Function FormatSSN(ByVal str)
Dim fssnRegEx
Set fssnRegEx = New RegExp
fssnRegEx.Pattern = "^(\d{3})[ -]?(\d{2})[ -]?(\d{4})$"
FormatSSN = fssnRegEx.Replace(str,"$1-$2-$3")
End Function

Function FormatZipCode(ByVal str)
If IsNull(str) Then Exit Function
Dim fzcRegEx
Set fzcRegEx = New RegExp
fzcRegEx.Pattern = "^(\d{5})[ .-]?(\d{4})$"
FormatZipCode = fzcRegEx.Replace(str,"$1-$2")
End Function

Function RemoveExtraSpaces(ByVal str)
If IsNull(str) Then Exit Function
Dim resRegEx
Set resRegEx = New RegExp
resRegEx.Pattern = "\s+"
resRegEx.Global = True
RemoveExtraSpaces = resRegEx.Replace(str," ")
End Function

Function RemoveLeadingZeroes(ByVal str)
Dim tempStr
tempStr = str
While Left(tempStr,1) = "0" AND tempStr <> ""
tempStr = Right(tempStr,Len(tempStr)-1)
Wend
RemoveLeadingZeroes = tempStr
End Function

Function RemoveSpaces(ByVal str)
If IsNull(str) Then Exit Function
Dim rsRegEx
Set rsRegEx = New RegExp
rsRegEx.Pattern = "\s"
rsRegEx.Global = True
RemoveSpaces = rsRegEx.Replace(str,"")
End Function

'*************************************************
' Type Conversion Functions
'*************************************************

Function EmptyToSpace(ByVal str)
If Len(str) = 0 OR IsNull(str) Then
EmptyToSpace = "&nbsp;"
Else
EmptyToSpace = Server.HTMLEncode(str)
End If
End Function

Function NullToEmpty(ByVal str)
If IsNull(str) Then Exit Function
NullToEmpty = str
End Function

Function NullToZero(ByVal str)
If IsNull(str) Then str = 0
NullToZero = str
End Function

'*************************************************
' Miscellaneous Functions
'*************************************************

Function Comma2Pipe(ByVal str)
If IsNull(str) Then Exit Function
Comma2Pipe = Replace(Replace(str,""",""","|"),chr(34),"")
End Function

Function CountWords(ByVal str)
If IsNull(str) Then Exit Function
Dim cwRegEx
Set cwRegEx = New RegExp
cwRegEx.Pattern = "\s+"
cwRegEx.Global = True
CountWords = UBound(Split(cwRegEx.Replace(Trim(Replace(str,vbCrLf," "))," ")," ")) + 1
End Function

Function DateExt(ByVal intDay)
If IsNumeric(intDay) Then
If CInt(intDay) <> intDay Then Exit Function
If (intDay < 11) OR (intDay > 13) Then
Select Case Right(intDay,1)
Case 1
DateExt = intDay & "st"
Case 2
DateExt = intDay & "nd"
Case 3
DateExt = intDay & "rd"
Case Else
DateExt = intDay & "th"
End Select
Else
DateExt = intDay & "th"
End If
End If
End Function

Function Date8(ByVal strDate)
If Not IsDate(strDate) Then Exit Function
Date8 = Year(strDate) & Right("0" & Month(strDate),2) & Right("0" & Day(strDate),2)
End Function

Function Date8Convert(ByVal str)
Dim tempDate
If Len(str) = 8 Then
tempDate = Right(str,2) & "/" & Mid(str,5,2) & "/" & Left(str,4)
If IsDate(tempDate) Then Date8Convert = CDate(tempDate)
End If
End Function

Function Date14(ByVal strDate)
If Not IsDate(strDate) Then Exit Function
Date14 = Year(strDate) & Right("0" & Month(strDate),2) & Right("0" & Day(strDate),2) & _
Right("0" & Hour(strDate),2) & Right("0" & Minute(strDate),2) & Right("0" & Second(strDate),2)
End Function

Function EnglishDate(byVal strDate)
Dim tempDateArray
If NOT IsDate(strDate) Then Exit Function
tempDateArray = Split(FormatDateTime(strDate,1)," ")
tempDateArray(2) = DateExt(CInt(Replace(tempDateArray(2),",",""))) & ","
EnglishDate = Join(tempDateArray," ")
End Function

Function Indent(iInput)
If CInt(iInput) <> iInput Then Exit Function
Indent = String(iInput,vbTab)
End Function

Function IsChecked(val1,val2)
If val1 = val2 Then IsChecked = " checked=""checked"""
End Function

Function IsSelected(val1,val2)
If val1 = val2 Then IsSelected = " selected=""selected"""
End Function

Function MaskCCNumber(ByVal sInput)
If Len(sInput) > 12 Then MaskCCNumber = String(Len(sInput) - 4,"*") & Right(sInput,4)
End Function

Function RequestFormat(str)
If IsNull(str) Then Exit Function
RequestFormat = Trim(RemoveExtraSpaces(Replace(str,vbTab," ")))
End Function

Function SQLSafe(ByVal str)
If IsNull(str) Then Exit Function
SQLSafe = Replace(str,"'","''")
End Function

Function VbCrLfToBreak(ByVal str)
If IsNull(str) Then Exit Function
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End Function
%>


What sucks is I don't even remember what half of these functions are for, now. I'm sure they are all useful in some manner though since this is obviously (to me) code that I used in day to day programming at my old job. :P

Old Pedant
04-02-2009, 09:22 AM
Oh, now, we can do a lot better for IsDateFormat!

Function IsDateFormat(str)
Dim idfRegEx
Set idfRegEx = New RegExp
idfRegEx.Pattern = "^(0?[1-9]|1[012])\/(0?[1-9]|[12]\d|3[01])\/(19|20}\s\s$"
IsDateFormat = idfRegEx.Test(str)
End Function

That's for USA mm/dd/yyyy format. You'd swap the first two (...) parts for UK format.

Actually, you *CAN* even do validation of Feb 29th (leap year only) if you are willing to write a monstrous regexp. <grin/>

But I think an easier way to do all that might be to just do:

Function IsDateFormat(str)
IsDateFormat = IsDate(str)
End Function

Admittedly, this will allow inputs such as "3/21/2009 13:53:22", but you can always then use DATEVALUE(str) to zap the time if you don't want it.

<grin style="monstrous" />

whammy
04-02-2009, 09:59 AM
I'm having a hard time determining how to put your example into theoretical practice since I don't yet have a test ASP server on this machine yet. I'm gonna set one up though. :)

I know my function worked well for what was necessary at the time - and I had to keep it simple since most of my former coworkers may have been very confused otherwise.

Not a one of them had used regular expressions to validate data before I started programming for that company (sigh). Instead they used paragraphs of if/then statements and the horrible ASP string parsing code for the most part.

Your idea seems very elegant though. :)

whammy
04-02-2009, 10:05 AM
If I remember correctly I used IsDate() outside of that function for some internal reason actually :)

Old Pedant
04-02-2009, 11:01 PM
Hey, I was mostly kidding you!

One thing wrong with using ISDATE() is that it will *PASS* such values as

31 March 2009
Mar 31, 2009
31/3/2009
2009-3-31

and probably much more.

Now, personally, I don't see anything wrong with that. If you use CDATE() on any of those, including on 3/31/2009, you will get the same internal value. But some people want to insist on 03/31/2009 for reasons I've never understood. If you store it in any DB, you will lose that format, anyway. But each to his/her own.

whammy
04-03-2009, 10:09 AM
If I remember correctly the values for dates in our database were stored like this:

20090403

...which I think was the purpose of some of those functions, having to spit the dates back out in a readable format.

Honestly it's likely irrelevant now, but that method of date storage did seem to have some redeeming values, even though it wasn't my idea. ;)

I remember having a lot of problems with ASP's IsDate() in practical everyday usage, which is likely why some of those functions are in my 'commonly used' code, though. How elegant my solutions were I am not sure of, especially after being out of the loop this long, lol.

Old Pedant
04-03-2009, 10:37 PM
If I remember correctly the values for dates in our database were stored like this:

20090403

Ugh. How ugly. You stored dates as NUMBERS? (Or as VARCHAR? either is equally ugly)

Or so you mean you were using MySQL? Yes, MySQL requires dates to be passed as either

INSERT INTO table (datefield) VALUES(20090403)
*OR*
INSERT INTO table (datefield) VALUES('2009-04-03')
*OR (omitting leading zeroes okay)*
INSERT INTO table (datefield) VALUES('2009-4-3')


If that's what the purpose of the functions was, then of course you needed to do something along the lines you showed. But if it war for any other DB... Ugh.

whammy
04-06-2009, 05:34 AM
That was actually one of the first things I asked about when I started at that company... I can pretty much quote myself saying 'Um, why is EVERYTHING stored as varchar?!?'. And it was with SQL Server 2000, NOT MySQL!

That's not the half of it though...

I ran into a lot of amusing and incredibly irresponsible things I ran into while working there as well...

For instance when I first learned about SQL injection attacks and ran a test on a partial copy of our biggest client's database and showed my boss that I could access all of the customer information (including credit card information) from an internet webpage!

My offer to fix the problem using stored procedures and cleaning up some of the bad code was shelved as the other programmers needed my help on more 'pressing' projects!

The highlight of that story though was the fact that as far as I know, I'm the only employee that actually received their promised severance pay when their position was eliminated from the company during 'restructuring' (after being bought out by another company).

I think they were afraid I'd spill the beans to their clients or even the BBB about that or perhaps some other bad practices that had been going on which I had objected to in the past. It's no wonder they are out of business now.

Ugly indeed!

Old Pedant
04-06-2009, 08:37 PM
Been there, done that...or things similar to that. *sigh*

The last place I worked is actually the first where we had QA people who understood web attacks and tested our site for them. What a pleasant change. JSP pages, though, not ASP or .NET.