c语言信用卡号的验证,验证信用卡函数(字符串处理)

验证还取决于信用卡的类型。

**************************************

name: credit card mod 10 validation

description:this function validates if

a credit card number "appears" to be val

id, depending on the type of card, and a

lso performing a mod 10 check on the num

bers.

by: lewis moten

inputs:ascardtype – type of credit car

d. (american express, discover, visa, ma

stercard)

ancardnumber – the number appearing on the card. dashes and spaces are ok. numbers are stripped from

the data provided.

returns:returns a boolean (true/false)

determining if the number appears to be

valid or not.

assumes:the user needs to be able to lo

ok through the code and determine wich s

trings represent the cards. you can eith

er type out the entire card name (i.e. "

american express") or type in just a let

ter representing the card name (i.e. "a"

)

side effects:just because the function

returns that the card is valid, there ar

e several other things that are not bein

g validated.

date – make sure the card has not expired

active account – this script does not communicate with any banks to determine if the account number is

active

authorization – again, this script does not communicate with any banks to determine if the card has

authorization to purchase a product.

warranty:

code provided by planet source code(tm)

(www.planet-source-code.com) as is, wi

thout warranties as to performance, fitn

ess, merchantability,and any other warra

nty (whether expressed or implied).

terms of agreement:

by using this source code, you agree to

the following terms…

1) you may use this source code in per

sonal projects and may compile it into a

n .exe/.dll/.ocx and distribute it in bi

nary format freely and with no charge.

2) you may not redistribute this sourc

e code (for example to a web site) witho

ut written permission from the original

author.failure to do so is a violation o

f copyright laws.

3) you may link to this code from anot

her website, provided it is not wrapped

in a frame.

4) the author of this code may have re

tained certain additional copyright righ

ts.if so, this is indicated in the autho

rs description.

**************************************

function iscreditcard(byref ascardtype, byref ancardnumber)

performs a mod 10 check to make sure the credit card number

appears valid

developers may use the following numbers as dummy data:

visa: 430-00000-00000

american express: 372-00000-00000

mastercard: 521-00000-00000

discover: 620-00000-00000

dim lsnumber credit card number stripped of all spaces, dashes,

etc.

dim lschar an individual character

dim lntotal sum of all calculations

dim lndigit a digit found within a credit card number

dim lnposition identifies a character position in a string

dim lnsum sum of calculations for a specific set

default result is false

iscreditcard = false

====

strip all characters that are not numbers.

====

loop through each character inthe card number submited

for lnposition = 1 to len(ancardnumber)

grab the current character

lschar = mid(ancardnumber, lnposition, 1)

if the character is a number, append it to our new number

if isnumeric(lschar) then lsnumber = lsnumber & lschar

next lnposition

====

the credit card number must be between 13 and 16 digits.

====

if the length of the number is less then 13 digits, then exit the routine

if len(lsnumber) < 13 then exit function

if the length of the number is more then 16 digits, then exit the routine

if len(lsnumber) > 16 then exit function

====

the credit card number must start with:

4 for visa cards

37 for american express cards

5 for mastercards

6 for discover cards

====

choose action based on type of card

select case lcase(ascardtype)

visa

case "visa", "v"

if first digit not 4, exit function

if not left(lsnumber, 1) = "4" then exit function

american express

case "american express", "americanexpress", "american", "ax", "a"

if first 2 digits not 37, exit function

if not left(lsnumber, 2) = "37" then exit function

mastercard

case "mastercard", "master card", "master", "m"

if first digit not 5, exit function

if not left(lsnumber, 1) = "5" then exit function

discover

case "discover", "discovercard", "discover card", "d"

if first digit not 6, exit function

if not left(lsnumber, 1) = "6" then exit function

case else

end select lcase(ascardtype)

====

if the credit card number is less then 16 digits add zeros

to the beginning to make it 16 digits.

====

continue loop while the length of the number is less then 16 digits

while not len(lsnumber) = 16

insert 0 to the beginning of the number

lsnumber = "0" & lsnumber

wend not len(lsnumber) = 16

====

multiply each digit of the credit card number by the corresponding digit of

the mask, and sum the results together.

====

loop through each digit

for lnposition = 1 to 16

parse a digit from a specified position in the number

lndigit = mid(lsnumber, lnposition, 1)

determine if we multiply by:

1 (even)

2 (odd)

based on the position that we are reading the digit from

lnmultiplier = 1 + (lnposition mod 2)

calculate the sum by multiplying the digit and the multiplier

lnsum = lndigit * lnmultiplier

(single digits roll over to remain single. we manually have to do this.)

if the sum is 10 or more, subtract 9

if lnsum > 9 then lnsum = lnsum – 9

add the sum to the total of all sums

lntotal = lntotal + lnsum

next lnposition

====

once all the results are summed divide

by 10, if there is no remainder then the credit card number is valid.

====

iscreditcard = ((lntotal mod 10) = 0)

end function iscreditcard

asp精品屋 from http://www.planet-source-code.com/vb/scripts/showcode.asp?lngwid=4&txtcodeid=6267

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值