Click here to Skip to main content
15,666,844 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I'm currently develop a program, so I would like to prevent user to enter random characters like "asalsfjsalkfjsakl" or "sssssss" or "ljlkjlkjlkjklj" I tried to catch this problem as follow:

Public Function checkCharacters(ByVal str As String) As String
        Dim temp As String = ""
        Dim f As String = ""
        Dim s As String = ""
        For i = 0 To str.Length - 1
            For j = i + 1 To str.Length - 2
                If str(i) = str(j) Then
                    temp += str(i).ToString()
                    If temp.Length >= 4 Then
                        Return temp.ToString()
                    End If
                    temp += ""
                End If
        Return ""
    End Function

    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        Dim str As String = txtStringtoCheck.Text
        Dim sd As New StringsDelimiter
        Dim result As String = checkCharacters(str)
            If result.Length >= 4 Then
                MsgBox("Please entered repeat characters " & result.ToString())
            End If
        Catch ex As Exception
        End Try
    End Sub

I was tried to prevent user to enter occurrence characters in a TextBox but it seem my code still not what I want.

so please are there anyway to prevent this kind of occurrence characters?

Thank you in advance.
Tomas Takac 14-Oct-15 3:38am    
Interesting. Why do you need this? It's not trivial. It's easy enough to reject a string of one repeated character. Finding repetitions is harder but still doable. But how do you decide that a string needs to be rejected because it's "random characters"? My name is composed of "random characters", so is yours. In any way you need to write more code, a lot more. Or adapt an existing solution.

My idea how to approach this would be to build a set of rules, each rule would examine the string and assign score, e.g.
* sequence of one char -> 1000
* repeated substring -> 20
* word longer than 10 chars -> 10
etc. then you sum the scores and if you reach a threshold you reject the input. But I'm sure you can find an existing rule engine to implement this.
CeebLaj Thoj 14-Oct-15 4:43am    
Thank you for you suggestion, after I tried all possible way to prevent this kind of input there is no way, so the only way to prevent is repetitions characters and I already done for that.

So anyway thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900