2016-10-14 24 views
1

如描述中所述,我需要驗證用戶輸入以確保它至少包含6個字符並且包含1個數字字符和字母表中的1個字符。驗證輸入有問題。需要驗證是否至少有一個數字字符和一個字母字符

到目前爲止,我已經得到了長度驗證工作,但似乎無法讓我的數字驗證正常工作。如果除了數字之外什麼都不輸入,它可以工作,但是如果我把字母放在IE abc123中,它將不會識別出有數字存在。

Public Class Form1 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     If txtPassword.TextLength < 6 Then 
      lblError.Text = "Sorry that password is too short." 
     ElseIf txtPassword.TextLength >= 6 Then 
      Dim intCheck As Integer = 0 
      Integer.TryParse(txtPassword.Text, intCheck) 
      If Integer.TryParse(txtPassword.Text, intCheck) Then 
       lblError.Text = "Password set!" 
      Else 
       lblError.Text = "Password contains no numeric characters" 
      End If 
     End If 
    End Sub 
End Class 
+0

使用此http://stackoverflow.com/a/14850765/1890983 –

回答

0

正則表達式怎麼樣?

using System.Text.RegularExpressions; 

private static bool CheckAlphaNumeric(string str)   { 
    return Regex.Match(str.Trim(), @"^[a-zA-Z0-9]*$").Success;   
} 

如果您剛剛驗證複雜的密碼,那麼這將做到這一點。

- 必須是至少6個字符

- 必須含有至少一個一個小寫字母,

-One大寫字母,

-One位和一個特殊字符

- 有效特殊字符 - @#$%^ & + =

Dim MatchNumberPattern As String = "^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" 
    If txtPasswordText.Trim <> "" Then 
     If Not Regex.IsMatch(txtPassword.Text, MatchNumberPattern) Then 
      MessageBox.Show("Password is not valid") 
     End If 
    End If 
0

您可能需要使用正則表達式來驗證,如果輸入中包含大/小寫字母字符,數字,特殊字符。

相關問題