请选择 进入手机版 | 继续访问电脑版
查看: 184|回复: 0

[.NET开发] Luhn算法

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 17:52:09
<无详细内容>
  1. <%
  2. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  3. ' 94305, USA.
  4. ' Luhn (mod 10) algorithm
  5. function luhn(byVal input)
  6. dim sum
  7. dim odd
  8. dim currentDigit
  9. dim regEx
  10. input = CStr(input)
  11. sum = 0
  12. odd = CBool(len(input) mod 2)
  13. ' Remove any non-numeric characters.
  14. if not isNumeric(input) then
  15. set regEx = new RegExp
  16. regEx.Global = true
  17. regEx.Pattern = "\D"
  18. input = regEx.Replace(input, "")
  19. set regEx = nothing
  20. end if
  21. ' Calculate sum of digits.
  22. for i = 1 to len(input)
  23. currentDigit = cint(mid(input, i, 1))
  24. if odd then
  25. sum = sum + currentDigit
  26. else
  27. if currentDigit * 2 > 9 then
  28. sum = sum + (currentDigit * 2 - 9)
  29. else
  30. sum = sum + (currentDigit * 2)
  31. end if
  32. end if
  33. odd = not odd
  34. next
  35. ' Check validity.
  36. if sum mod 10 = 0 then
  37. luhn = true
  38. else
  39. luhn = false
  40. end if
  41. end function
  42. ' Unit test - expected result is true
  43. if luhn("446667651") then
  44. response.write "true"
  45. else
  46. response.write "false"
  47. end if
  48. %>
复制代码



上一篇:VB 中创建表格(2)
下一篇:Soundex
回复

使用道具 举报