Vb.net用Luhn's Algorthim辨别信用卡真假
2007-11-07 00:13
330 查看
Public Function ValidateLuhn(ByVal value As String) As Boolean
'I am using Luhn's Algorthim to validate the inserted number
'i then call this function later when i need to use it
Dim CheckSum As Integer = 0
Dim DoubleFlag As Boolean = (value.Length Mod 2 = 0)
Dim Digit As Char
Dim DigitValue As Integer
For Each Digit In value
DigitValue = Integer.Parse(Digit)
If DoubleFlag Then
DigitValue *= 2
If DigitValue > 9 Then
DigitValue -= 9
End If
End If
CheckSum += DigitValue
DoubleFlag = Not DoubleFlag
Next
Return (CheckSum Mod 10 = 0)
这个是Luhn算法的函数代码,当然还需要输入让用户仅能输入数字
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If TextBox1.Text.Length >= 4 Then e.Handled = True
If Not IsNumeric(e.KeyChar) Then e.Handled = True 'make users input 4 numbers only.
End Sub
再接下来就是按钮的代码
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MsgBox("please input you card number")
Else
If TextBox1.Text.Length = 4 Or TextBox2.Text.Length = 4 Or TextBox3.Text.Length = 4 Or TextBox3.Text.Length <= 8 Then 'most of the credit card number is more than 13 and less than 20
If ValidateLuhn(TextBox1.Text + TextBox2.Text + TextBox3.Text + TextBox4.Text) Then 'use Luhn's Algorthim check the credit card
MessageBox.Show("the Number is correct")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Else
MessageBox.Show("the Number is not correct")
TextBox1.Text = ""
TextBox2.Text = " "
TextBox3.Text = ""
TextBox4.Text = ""
End If
Else
MessageBox.Show("your input is illegal")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
End If
不能上传图片...
'I am using Luhn's Algorthim to validate the inserted number
'i then call this function later when i need to use it
Dim CheckSum As Integer = 0
Dim DoubleFlag As Boolean = (value.Length Mod 2 = 0)
Dim Digit As Char
Dim DigitValue As Integer
For Each Digit In value
DigitValue = Integer.Parse(Digit)
If DoubleFlag Then
DigitValue *= 2
If DigitValue > 9 Then
DigitValue -= 9
End If
End If
CheckSum += DigitValue
DoubleFlag = Not DoubleFlag
Next
Return (CheckSum Mod 10 = 0)
这个是Luhn算法的函数代码,当然还需要输入让用户仅能输入数字
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If TextBox1.Text.Length >= 4 Then e.Handled = True
If Not IsNumeric(e.KeyChar) Then e.Handled = True 'make users input 4 numbers only.
End Sub
再接下来就是按钮的代码
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MsgBox("please input you card number")
Else
If TextBox1.Text.Length = 4 Or TextBox2.Text.Length = 4 Or TextBox3.Text.Length = 4 Or TextBox3.Text.Length <= 8 Then 'most of the credit card number is more than 13 and less than 20
If ValidateLuhn(TextBox1.Text + TextBox2.Text + TextBox3.Text + TextBox4.Text) Then 'use Luhn's Algorthim check the credit card
MessageBox.Show("the Number is correct")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Else
MessageBox.Show("the Number is not correct")
TextBox1.Text = ""
TextBox2.Text = " "
TextBox3.Text = ""
TextBox4.Text = ""
End If
Else
MessageBox.Show("your input is illegal")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
End If
不能上传图片...
相关文章推荐
- VB.NET Developer's Guide
- 在ASP 调用 VB.net 编写的dll 出错 提示:Microsoft VBScript 运行时错误 错误 '800a01ad' ActiveX 部件不能创建对象
- VB.NET&C#&VB较量
- VB.NET中实现"关机/休眠/重启/注销"的类
- 面向对象程序设计——VB.NET&C# 篇
- O'Reilly - .NET Windows Forms in a Nutshell [免费下载 经典英文原版书]
- vb.net & 文本框为空提示,一键清空文本框所有内容
- vb.net 教程 3-5 窗体编程 对话框3 ColorDialog & FontDialog
- tomcat 应用Message file 'oracle.net.mesg.Message' is missing问题处理
- ASP.NET AJAX 错误:'Sys' 未定义解决方法
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- 'VB 中将信息写入到XML中,并从后台用ajax+jquery 取出信息
- Delete a file in use - Run an EXE Hidden - Run code in other process' memory)(vb)
- VB.NET机房收费 & 初感受
- asp.net程序 用户 'NT AUTHORITY/NETWORK SERVICE' 登录失败解决两个方法
- vb.net 教程 3-4 窗体编程 公共控件1 Button、Label、TextBox & CheckBox
- java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes 用jspSmartUpload.jar下载 出错
- .Net 's delegates and events
- 关于 sessionState mode='InProc' 不用启动ASP.NET 服务
- struts2 action 命名冲突导致的Unexpected Exception caught setting 'act' on 'class com.quamnet.backend.cms.ac