Featured Post

Attaching a footer to the bottom of the page

I've been messing around with positioning a footer at the bottom of a page for a few hours now and finally after trial and error have a workable solution that seems to be cross browser compliant. My first thoughts were to absolutely position the footer element at the bottom of the page, but I knew that...

Read More

ASP Search Stemmer Class

Posted by chelfers | Posted in ASP, Web | Posted on 02-08-2009

Tags: , , ,

0

The original stemmer class was developed by Martin Porter to bring words back to their word stems. For example "abilities" would stem to "able", "smelling" to "smell", "I'm awesome" to "damn straight", etc.

I couldn't find a Classic ASP version of this so I ported a port ( I'm not even sure if that's legal! ).

example usage

set Search = new Stemmer      tmp_string = Search.Stem( "abilities" )      response.write tmp_string set Search = nothing

This should produce an echo of "able". Below is a dictionary list of words you can test the class out with, let me know if you find any bugs or issues.

Download ASP Stemmer 1.0.0
Download Dictionary File

How to check for legit credit card numbers

Posted by chelfers | Posted in ASP, Web | Posted on 30-07-2009

Tags: ,

0

I think the title says it all; I've seen many implementations for checking credit card numbers and in my opinion there is really only one that you need and actually works.

Sadly, still in use today by many code examples that I've run across is the good old

if ( first_digit = 4 ) then "visa" elseif ( first_digit = 5 ) then "mastercard" elseif ( first_digit = 3 ) then "amex" elseif ( first_digit = 6 ) then "discover" else "invalid card" end if

/facepalm

This isn't entirely bad ( yes it is, I'm just trying to be nice ), most likely those first digits will always be associated with the cards mentioned above, the problem is the numbers that follow.

"But I found one that checks and second and third numbers!"

Awesome, one tiny step closer to almost not really getting anywhere!

Obviously I'm leading up to the one and only Luhn algorithm which is basically a simple checksum formula used to validate a few different sequences of numbers, for example credit card account numbers.

Straight from Wikipedia here is what is going on with the algorithm

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
2. Sum the digits of the products together with the undoubled digits from the original number.
3. If the total ends in 0 (put another way, if the total modulo 10 is congruent to 0), then the number is valid according to the Luhn formula; else it is not valid.

As an illustration, if the account number is 49927398716, it will be validated as follows:

1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
3. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.

Below is an ASP version I've ported, enjoy.

ACCOUNT = "4500000000000001" alt = false sum = 0     for i = len( ACCOUNT ) to 1 step - 1             digit = mid( ACCOUNT, i, 1 )                     if ( alt = true ) then                 digit = digit * 2                     if ( digit > 9 ) then digit = digit - 9 end if             end if         sum = ( sum + digit ) mod 10                                                 if ( alt = true ) then alt = false else alt = true     next if ( sum = 0 ) then   'legit else   'did not pass the test end if