2017-01-15 2 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 
관련 문제