㈠ VB程序編寫賬號和密碼的驗證程序
PrivateSubCommand1_Click()
IfIsNumeric(Text1.Text)AndLen(Text1.Text)=6AndText2.Text="good"Then
MsgBox"輸入正確,驗證通過",vbOKCancel,"驗證通過"
Else
IfMsgBox("賬號或密碼錯誤",vbRetryCancel,"驗證錯誤")=vbRetryThen
Text1.Text=""
Text2.Text=""
Text1.SetFocus
Else
End
EndIf
EndIf
EndSub
PrivateSubForm_Load()
Text1.PasswordChar="*"
EndSub
㈡ 用VB編寫「密碼校驗」程序的設計
完整代碼如下,初始化都已經做好啦,添加控制項試試吧:
Dim NCount As Integer
Private Sub Form_Load()
'初始化控制項信息
Caption = "密碼校驗"
Label1.Caption = "請輸入密碼:"
Text1.Text = ""
Label2.ForeColor = &HFF&
Label2.Alignment = 2
Label2.FontName = "宋體"
Label2.FontSize = 15.75
Label2.AutoSize = True
Label2.Visible = False
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Text1.Text) > 6 And KeyAscii <> 8 And KeyAscii <> 13 Then
KeyAscii = 0
End If
If KeyAscii = 13 And Text1.Text = "1234567" Then
Label2.Caption = "歡迎光臨!"
Label2.Visible = True
ElseIf KeyAscii = 13 Then
If NCount <> 1 Then
Label2.Visible = True
Label2.Caption = "密碼不符,請再輸入一遍!"
Text1.Text = ""
NCount = NCount + 1
Else
Label2.Visible = True
Label2.Caption = "非法用戶,請推出程序!"
Text1.Text = ""
Text1.Enabled = False
End If
End If
End Sub
㈢ 用VB語言編制一個帳號與密碼的檢驗程序
Option Explicit
Const MaxLogTimes As Integer = 3
Private Sub cmdCancel_Click()
Dim intResult As Integer
'請求用戶確認是否真的退出系統登錄
intResult = MsgBox("你選擇了退出系統登錄,退出將不能啟動管理系統!" & vbCrLf & "是否真的退出?", vbYesNo, "登錄驗證")
If intResult = vbYes Then End '根據用戶選擇結束應用程序
End Sub
Private Sub cmdOK_Click()
Static intLogTimes As Integer '用於保存用戶請求驗證的次數
Dim intChecked As Integer, strName As String, strPassword As String
intLogTimes = intLogTimes + 1 '計算登錄次數
If intLogTimes > MaxLogTimes Then
'超過允許的登錄次數,顯示提示信息
MsgBox "你已經超過允許驗證次數!" & vbCr & "應用程序將結束!", vbCritical, "登錄驗證"
End '結束應用程序
Else '進一步驗證登錄信息的合法性
strName = Trim(txtUserName.Text) '獲得輸入的用戶名
strPassword = Trim(txtPassWord.Text) '獲得輸入的口令
'檢驗用戶名和口令的合法性,並根據檢驗返回值執行相應的操作
if(strName ="123456")
if(strPassword="VB6") '口令正確
Unload Me '卸載登錄窗體
MsgBox "登錄成功,將啟動系統程序!", vbInformation, "登錄驗證"
'通常在此放置顯示系統主窗體的語句,例如
'frmMain.Show
else
MsgBox "口令錯誤,請重新輸入!", vbCritical, "登錄驗證"
txtPassWord.Text = ""
txtPassWord.SetFocus
end if
else '用戶不是系統用戶
MsgBox "<" & strName & ">不是系統用戶,請檢查用戶名輸入是否正確!", vbCritical, "登錄驗證"
txtUserName.Text = ""
txtPassWord.Text = ""
txtUserName.SetFocus
End If
End If
End Sub
'這是你要的重試 我覺得很沒有必要
Private Sub cmdReset_Click()
txtPassWord.Text = ""
txtPassWord.SetFocus
End Sub
說明:
1.賬號密碼一般是通過資料庫調用的,這個程序只是用於娛樂或者測試
2.密碼框輸出顯示請在VB的密碼框屬性設置里更該,設password為*即可
3.好好讀讀代碼,你就明白代碼怎麼用了