File : ccard.inc. ID : 2802
Skin : Default | Sons-of-obsidian | Sunburst | Highlighter | Frame
<%
' Credit Card check routine for ASP (c) 1998 by Click Online
' http://www.click-online.de info@click-online.de
function trimtodigits(tstring)
'removes all chars except of 0-9
  s="" 
  ts=tstring
  for x=1 to len(ts)
    ch=mid(ts,x,1)
    if asc(ch)>=48 and asc(ch)<=57 then
      s=s & ch
    end if
  next
  trimtodigits=s
end function
function checkcc(ccnumber,cctype)
  'checks credit card number for checksum,length and type
  'ccnumber= credit card number 
  ' (all useless characters are being removed before check)
  'cctype: (ประเภทบัตร)
  '  [V]ISA [M]astercard/Eurocard [A]merican Express 
  '  [D]iners Club / Carte Blanche [D]iscover [E]nRoute [J]CB
  'returns: (ผลการตรวจสอบ)  
  '  checkcc=0 (Bit0) : card valid
  '  checkcc=1 (Bit1) : wrong type
  '  checkcc=2 (Bit2) : wrong length
  '  checkcc=4 (Bit3) : wrong checksum (MOD10-Test)
  '  checkcc=8 (Bit4) : cardtype unknown
  ctype=ucase(cctype)
  ' เก็บค่าเงื่อนไขไว้ ตรวจสอบตามประเภทบัตร
  select case ctype
    case "V"
      cclength="13;16"
      ccprefix="4"
    case "M"
      cclength="16"
      ccprefix="51;52;53;54;55"
    case "A"
      cclength="15"
      ccprefix="34;37"
    case "C"
      cclength="14"
      ccprefix="300;301;302;303;304;305;36;38"
    case "D"
      cclength="16"
      ccprefix="6011"
    case "E"
      cclength="15"
      ccprefix="2014;2149"
    case "J"
      cclength="15;16"
      ccprefix="3;2131;1800"
    case else
      cclength=""
      ccprefix=""
  end select
  prefixes=split(ccprefix,";",-1)
  lengths=split(cclength,";",-1)
  number=trimtodigits(ccnumber)
  prefixvalid=false
  lengthvalid=false
' ตรวจประเภทบัตร
  for each prefix in prefixes
    if instr(number,prefix)=1 then
      prefixvalid=true
    end if
  next  
' ตรวจความยาว เช่น visa ต้องได้ 13
  for each length in lengths
    if cstr(len(number))=length then
      lengthvalid=true
    end if
  next
  result=0
  if not prefixvalid then
    result=result+1
  end if  
  if not lengthvalid then
    result=result+2
  end if  
' ตรวจ gsum การนำเลขทั้งหมดมาคำนวณ ว่ารูปแบบถูกต้องหรือไม่
  qsum=0
  for x=1 to len(number)
    ch=mid(number,len(number)-x+1,1)
    if x mod 2=0 then
      sum=2*cint(ch)
      qsum=qsum+(sum mod 10)
      if sum>9 then 
        qsum=qsum+1
      end if
    else
      qsum=qsum+cint(ch)
    end if
  next
  if qsum mod 10<>0 then
    result=result+4
  end if
  if cclength="" then
    result=result+8
  end if
  checkcc=result
end function
%>