2017-01-15 13 views
0

私はいくつかの調査をしましたが、答えは出ていませんでした。私はRegexメソッドについて読んでいますが、私はこれで実際に新しいです、そして、私はそれを聞いたことがありません。文字列にVB.netで特定のcharcatersが含まれているかどうかを確認するにはどうすればよいですか?

私がしようとしているのは、ユーザーが大文字のS、大文字のSの後の8つの数字だけを含むパスワード(私の場合は「学生番号」と呼ぶ)を入力したかどうかを確認することです。最終的に特殊文字*(特にこの順序で)。

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click 

     Dim InvalidName As Integer = 0 
     Dim InvalidStudentNumber_Numeric As Integer = 0 

     For intIndex = 0 To txtName.Text.Length - 1 
      If IsNumeric(txtName.Text.Substring(intIndex, 1)) Then 
       InvalidName = 1 
      End If 
     Next 

     For intIndex = 0 To txtStudentNumber.Text.Length - 1 
      If IsNumeric(txtStudentNumber.Text.Substring(intIndex, 1)) Then 
       InvalidStudentNumber_Numeric += 1 

      End If 
     Next 

     If InvalidName <> 0 Then 
      MessageBox.Show("The name entered does not meet the characters criteria. Provide a non-numeric name, 10 characters or longer.", 
          "Invalid Information: Name") 
      txtName.Focus() 

     ElseIf InvalidStudentNumber_Numeric <> 8 Then 
      MessageBox.Show("The student number entered does not meet the characters criteria. Provide a non-numeric student number, 10 characters long.", 
          "Invalid Information: Student Number") 
      txtStudentNumber.Focus() 

ので、学生の名前の通り、私は何の問題を持っていませんが、パスワードは私を取得するものである:

私はすでにこれをプログラムします。私はすでに数字があるかどうかを知る方法を知っていましたが(8でなければなりません)、文字列の最後に大文字のSを検索する方法がわかりません。

+0

プログラミングが初めての方は、10フィートポールのセキュリティ関連の面に触れないでください。重大な失敗が差し迫っている。 – zx485

答えて

1

正規表現の必要はありません。

Public Function IsValidStudentNumber(ByVal id As String) As Boolean 
    ' Note that the `S` and the `*` appear to be common to all student numbers, according to your definition, so you could choose to not have the users enter them if you wanted. 
    Dim number As Int32 = 0 

    id = id.ToUpper 

    If id.StartsWith("S") Then 
     ' Strip the S, we don't need it. 
     ' Or reverse the comparison (not starts with S), if you want to throw an error. 
     id = id.Substring(1) 
    End If 

    If id.EndsWith("*") Then 
     ' Strip the *, we don't need it. 
     ' Or reverse the comparison (not ends with *), if you want to throw an error. 
     id = id.Substring(0, id.Length - 1) 
    End If 

    If 8 = id.Length Then 
     ' Its the right length, now see if its a number. 
     If Int32.TryParse(id, number) Then 
      Return True 
     End If 
    End If 
    Return False 
End Function 
関連する問題