locked
validate filename RRS feed

  • Question

  • User-809753952 posted

    I want to check that a given filename  contains only English letters and/or other valid characters. It should not contain any other language letters.

    Please help.

    Tuesday, December 1, 2020 10:52 AM

All replies

  • User-189459990 posted

    First, what is the type of your project and what is language used? Here is a simple demo with VB.NET maybe you can refer to.

    To check that a given filename  contains only English letters, you can use Regex to achieve it.

    ''' <summary>
    ''' contains only English, will return true
    ''' </summary>
    Public Function IsOnlyLetter(filename As String) As Boolean
        Dim pattern As String = "^[A-Za-z]+$"
        Dim regex As Regex = New Regex(pattern)
        Return regex.IsMatch(filename)
    End Function

    And if you want to include English letters and valid characters, such as “!,@,#”, and exclude other language characters, you can try to check whether the characters in the string are all included in ASCII. As ASCII ranges from 0 - 127, you can check it as followed.

    ''' <summary>
    ''' contains any other language letters, will return false
    ''' </summary>
    Public Function IsOnlyASCII(filename As String) As Boolean
        Dim charArr As Char() = filename.ToCharArray()
        For Each ch As Char In charArr
            If Convert.ToInt32(ch) < 128 Then
                Continue For
            Else
                Return False
            End If
        Next
        Return True
    End Function

    Besides, you can also call UTF8Encoding.GetByteCount Method to get the number of bytes.

    ''' <summary>
    ''' contains any other language letters, will return false
    ''' </summary>
    Public Function IsOnlyASCII2(filename As String) As Boolean
        Return System.Text.Encoding.UTF8.GetByteCount(filename) = filename.Length
    End Function
    Wednesday, December 2, 2020 5:51 AM
  • User-809753952 posted

    Dear kyle0418!

    Thaks for your reply. Your solution works almost. But it is not satisfying all my conditions.

     It allows  somec characters like ?, \ , {,/ , & . These are not generally allowed in filnames.

    How can we avoid them?

    Wednesday, December 2, 2020 9:01 AM
  • User-189459990 posted

    A simple way is excluding them via the corresponding encoding in ASCII (please query ASCII).

    Public Function IsOnlyASCII(filename As String) As Boolean
        Dim excludelist As List(Of Integer) = New List(Of Integer) From {63, 92, 123, 47, 38} ' ?, \ , {, /, & this is a list to store excluded charactor
        Dim charArr As Char() = filename.ToCharArray()
    
        For Each ch As Char In charArr
            If Convert.ToInt32(ch) < 128 Then
                If excludelist.Contains(Convert.ToInt32(ch)) Then
                    Return False
                Else
                    Continue For
                End If
            Else
                Return False
            End If
        Next
    
        Return True
    End Function

    Or improve regular expression. Here assuming that the filename can only contain alphanumeric and character‘-’. You can modify the regex like,

    Public Function IsOnlyLetter(filename As String) As Boolean
        Dim pattern As String = "^[A-Za-z0-9\-]+$" ' add \- in the expression
        Dim regex As Regex = New Regex(pattern)
        Return regex.IsMatch(filename)
    End Function

    Wednesday, December 2, 2020 9:30 AM