2016-10-14 15 views
1

説明で述べたように、ユーザー入力を検証して、6文字以上で、アルファベットから1文字と1文字。入力の検証に問題があります。少なくとも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;   
} 

複雑なパスワードの検証が完了したばかりの場合は、これを実行します。

少なくとも一つずつ小文字、

-One大文字、

-One桁を含む-Must少なくとも6つの文字

こと-Must 1つの特殊文字

個の-valid特殊文字がある - @#$は%^ & + =

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

入力は大文字/小文字のアルファ文字、数字、特殊文字が含まれている場合は、検証するために正規表現を使用することができます。

関連する問題