Announcement

Collapse
No announcement yet.

Limit InputBox To Text Only & X Characters

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Limit InputBox To Text Only & X Characters



    what be the most economical regex pattern which would limit a text stings length to x characters and only allow a to z, A to Z, 0 to 9, and these 8 common punctuation marks , . - _ ( ) ? '

    Thank you

  • #2
    Re: Regex Patern To Limit Text For Input Box

    Code:
    Sub TextOnly()
        Dim strReturn As String
        Dim lNumCheck As Long
        Dim bNumber As Boolean
        
        strReturn = Application.InputBox(Prompt:="Enter text", Type:=2)
        If strReturn = vbNullString Then Exit Sub
             
        If Len(strReturn) > 8 Then
             MsgBox "No more than 8 Characters"
             Run "TextOnly"
        End If
        
        For lNumCheck = 1 To Len(strReturn)
            bNumber = False
            bNumber = IsNumeric(Mid(strReturn, lNumCheck, 1))
                If bNumber = True Then
                    MsgBox "No numbers allowed"
                    Exit Sub
                End If
        Next lNumCheck
         
         
    End Sub
    Last edited by Dave Hawley; January 22nd, 2010, 09:00.

    Comment


    • #3
      Re: Limit InputBox To Text Only & X Characters

      The Like operator will test if any of the characters are not allowed.

      Code:
      If strReturn Like "*[!,.)'( _0-9A-Za-z(?)-]*" Then
          MsgBox "bad Character somewhere"
      End If

      Comment


      • #4
        Re: Limit InputBox To Text Only & X Characters

        Borrowing from http://www.ozgrid.com/forum/showthread.php?t=37624 I would like to suggest the following code to get you started in the right direction:

        Code:
        Const MAX_LENGTH As String = "2"
        
        Function RegExResult(strData As String) As String
            Dim RE As Object, REMatches As Object
             
            Set RE = CreateObject("vbscript.regexp")
            With RE
                .MultiLine = False
                .Global = False
                .IgnoreCase = True
                .Pattern = "([\w]|[,.\-\_()?\']){1," & MAX_LENGTH & "}"
            End With
             
            Set REMatches = RE.Execute(strData)
            If REMatches.Count > 0 Then
                If REMatches(0) = "" Then
                    RegExResult = "No match"
                Else
                    RegExResult = REMatches(0)
                End If
            Else
                RegExResult = "No match"
            End If
             
        End Function
        
        
        Sub TextOnly()
            Dim strReturn As String
            Dim lNumCheck As Long
            Dim bNumber As Boolean
             
            strReturn = Application.InputBox(Prompt:="Enter text", Type:=2)
            If strReturn = vbNullString Then Exit Sub
             
            Dim result As String
            result = RegExResult(strReturn)
            
            MsgBox "Input: " & strReturn & vbCr & "Filtered: " & result
            
        End Sub

        Comment


        • #5


          Re: Limit InputBox To Text Only & X Characters

          Originally posted by mikerickson
          The Like operator will test if any of the characters are not allowed.

          Code:
          If strReturn Like "*[!,.)'( _0-9A-Za-z(?)-]*" Then
              MsgBox "bad Character somewhere"
          End If
          Nice

          Comment

          Working...
          X