LOCAL lcCredNoString, lcSpacers, ln_i
LOCAL ln_Mod10, ln_pos, ln_digit
IF EMPTY(THIS.value)
THIS.lValidCard = .F.
THIS.cCardType = ''
RETURN
ENDIF
** Copy this value to a working variable
lcCredNoString = ALLTRIM(THIS.Value)
** Remove some caracters used as spacers
lcSpacers = ' -./*\'
FOR ln_i = 1 TO LEN(m.lcSpacers)
lcCredNoString = STRTRAN(lcCredNoString, ;
SUBSTR(m.lcSpacers, m.ln_i, 1) ,'')
ENDFOR
** Get all digits except the last one
lcCredNoString = SUBSTR(lcCredNoString, 1, LEN(lcCredNoString) - 1 )
** Calculate the modulus 10 for lcCreditNoString
m.ln_mod10 = 0
FOR m.ln_pos = 1 TO LEN(lcCredNoString)
m.ln_Digit =VAL( SUBSTR(lcCredNoString, m.ln_pos , 1) )
** If in odd numbered position, multiply by two
m.ln_digit = m.ln_digit * IIF(m.ln_pos % 2 = 0, 1, 2)
** If the result is greater than 10, add the two digits together
m.ln_digit = INT(m.ln_digit / 10) + m.ln_digit % 10
** Additonner les nombres ensemble
m.ln_mod10 = m.ln_mod10 + m.ln_digit
ENDFOR
m.ln_mod10 = ALLTRIM(STR(10 - (m.ln_mod10 % 10)))
m.ln_mod10 = IIF(m.ln_mod10 = '10', '0', m.ln_mod10)
** Prepare the return value
THIS.lValidCard = (RIGHT(ALLTRIM(THIS.value),1) = m.ln_mod10)
** Type of card
DO CASE
CASE LEFT(THIS.value,1) = '5'
THIS.cCardtype = "MasterCard"
CASE LEFT(THIS.value,1) = '4'
THIS.cCardtype = "Visa"
CASE LEFT(THIS.value,1) = '3'
THIS.cCardtype = "American Express"
OTHERWISE
THIS.cCardtype = "U"
ENDCASE
RETURN THIS.lValidCard