Results 1 to 11 of 11

Thread: Best way to accomplish this task

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Best way to accomplish this task

    Hi all,

    In some of my TextBoxes and editable ComboBoxes, I want to make sure that the end-user can only type characters and whitespace but not to allow whitespace without characters existing too.

    I have started to write a public sub and would like to know the best way of accomplishing this scenario check. This is what I have so far. Your ideas are welcome.
    vb Code:
    1. <CLSCompliant(True)> Public Sub WhiteSpaceAndLettersOnly(ByVal ctrl As Elegant.Ui.Control)
    2.         'This is going to be used on TextBoxes and editable ComboBoxes.
    3.         'This will not be used on txtWorkLog.
    4.         'Allow whitespace and letters only.
    5.         'Do not allow only whitespace without letters existing.
    6.         If TypeOf ctrl Is Elegant.Ui.TextBox Then 'TextBox.
    7.             If Not ctrl.Name Is "txtWorkLog" Then 'Not txtWorkLog.
    8.                 If Not String.IsNullOrEmpty(ctrl.Text) Then 'Isnt empty.
    9.  
    10.                 End If
    11.             ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
    12.                 If DirectCast(ctrl, Elegant.Ui.ComboBox).Editable = True Then
    13.  
    14.                 End If
    15.             End If
    16.         End If
    17.     End Sub

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    OK, I have been researching Regex and this works for letters and spaces but if I type in only spaces, the ErrorProvider doesn't come up. That's expected but how do you make sure spaces are not entered without the existence of letters?
    vb Code:
    1. <CLSCompliant(True)> Private Sub txtName_TextChanged(ByVal sender As Object, _
    2.                                                          ByVal e As System.EventArgs) Handles txtName.TextChanged
    3.  
    4.         'Make sure the name is auto titlecased.
    5.         TitleCaseTextBox(txtName)
    6.  
    7.         Dim ErrProv As New ErrorProvider
    8.  
    9.         'Make sure only upper case, lower case and spaces allowed.
    10.         If Not New Regex("^[a-zA-Z\s]*$").IsMatch(txtName.Text) Then
    11.             ErrProv.SetError(txtName, "Value can only contain letters and spaces.")
    12.         Else
    13.             ErrProv.SetError(txtName, "")
    14.         End If

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Best way to accomplish this task

    I ain't well-versed with regex, but a simple method could be to check for existence of space and for characters/alphabets separately and if both pass, only then allow. Also have a separate function that takes in a string/text type parameter and checks for the existence of spaces and characters, and return true/false. From the control's textchanged/keypress/lostfocus event, call this function and take action appropriately. This way all the checking will be centralized.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Best way to accomplish this task

    Perhaps check for an empty string which is nothing entered or simply spaces entered.

    Code:
    If txtName.Text.Trim.Length > 0 Then
        If Not New Regex("^[a-zA-Z\s]*$").IsMatch(txtName.Text) Then
            ErrProv.SetError(txtName, "Value can only contain letters and spaces.")
        Else
            ErrProv.SetError(txtName, "")
        End If
    Else
        ErrProv.SetError(txtName, "")
    End If

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    Quote Originally Posted by kevininstructor View Post
    Perhaps check for an empty string which is nothing entered or simply spaces entered.

    Code:
    If txtName.Text.Trim.Length > 0 Then
        If Not New Regex("^[a-zA-Z\s]*$").IsMatch(txtName.Text) Then
            ErrProv.SetError(txtName, "Value can only contain letters and spaces.")
        Else
            ErrProv.SetError(txtName, "")
        End If
    Else
        ErrProv.SetError(txtName, "")
    End If
    I haven't tested this yet but does it look right?
    vb Code:
    1. Public Function WhitespaceLettersOnly(ByVal ctrl As Elegant.Ui.Control) As Boolean
    2.         WhitespaceLettersOnly = False
    3.  
    4.         Dim ErrProv As New ErrorProvider
    5.  
    6.         'Make sure only upper case, lower case and spaces allowed.
    7.         'No spaces allowed without the existence of letters.
    8.         If TypeOf ctrl Is Elegant.Ui.TextBox Then
    9.             If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _
    10.             AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then
    11.                 If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then
    12.                     ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    13.                     WhitespaceLettersOnly = WhitespaceLettersOnly Or True
    14.                 ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
    15.                     ErrProv.SetError(ctrl, "You must supply a value.")
    16.                     WhitespaceLettersOnly = WhitespaceLettersOnly Or True
    17.                 Else
    18.                     ErrProv.SetError(ctrl, "")
    19.                     WhitespaceLettersOnly = WhitespaceLettersOnly Or False
    20.                 End If
    21.             End If
    22.         ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
    23.             If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then
    24.                 If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _
    25.                 AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then
    26.                     If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    27.                         ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    28.                         WhitespaceLettersOnly = WhitespaceLettersOnly Or True
    29.                     ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
    30.                         ErrProv.SetError(ctrl, "You must supply a value.")
    31.                         WhitespaceLettersOnly = WhitespaceLettersOnly Or True
    32.                     Else
    33.                         ErrProv.SetError(ctrl, "")
    34.                         WhitespaceLettersOnly = WhitespaceLettersOnly Or False
    35.                     End If
    36.                 End If
    37.             End If
    38.         End If
    39.  
    40.         Return WhitespaceLettersOnly
    41.     End Function

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    Come to think about it, a function won't be the best option here. Maybe a Public Sub because it'll be used to check as the end-user types.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    This is what I have now. The only thing is, it's not showing the ErrorProvider regardless of situation.
    vb Code:
    1. Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control)
    2.         bCheckFailed = True
    3.  
    4.         Dim ErrProv As New ErrorProvider
    5.  
    6.         If TypeOf ctrl Is Elegant.Ui.TextBox Then
    7.             'Something is present but not just spaces.
    8.             If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _
    9.             AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then
    10.                 'Check for a-z, A-Z and whitespace (\s).
    11.                 'Make sure only upper case, lower case and spaces allowed.
    12.                 'No spaces allowed without the existence of letters.
    13.                 If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then
    14.                     ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    15.                     bCheckFailed = bCheckFailed Or True
    16.                     'No text entered.
    17.                 ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
    18.                     ErrProv.SetError(ctrl, "You must supply a value.")
    19.                     bCheckFailed = bCheckFailed Or True
    20.                 Else
    21.                     ErrProv.SetError(ctrl, "")
    22.                     bCheckFailed = bCheckFailed Or False
    23.                 End If
    24.             End If
    25.         ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
    26.             'ComboBox is editable.
    27.             If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then
    28.                 'Something is present but not just spaces.
    29.                 If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _
    30.                 AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then
    31.                     'Check for a-z, A-Z and whitespace (\s).
    32.                     'Make sure only upper case, lower case and spaces allowed.
    33.                     'No spaces allowed without the existence of letters.
    34.                     If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    35.                         ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    36.                         bCheckFailed = bCheckFailed Or True
    37.                         'No text entered.
    38.                     ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
    39.                         ErrProv.SetError(ctrl, "You must supply a value.")
    40.                         bCheckFailed = bCheckFailed Or True
    41.                     Else
    42.                         ErrProv.SetError(ctrl, "")
    43.                         bCheckFailed = bCheckFailed Or False
    44.                     End If
    45.                 End If
    46.             End If
    47.         End If
    48.     End Sub

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    OK, I am getting somewhere. I am able to get it to work when the end user only types in spaces or numbers. But the error doesn't go away when I type in something valid. Time to go through my logic. Would love a second pair of eyes. Been coding straight for nine hours now. :-)
    vb Code:
    1. Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control)
    2.         bCheckFailed = True
    3.  
    4.         Dim ErrProv As New ErrorProvider
    5.  
    6.         If TypeOf ctrl Is Elegant.Ui.TextBox Then
    7.             'Something is present but not just spaces.
    8.             If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _
    9.             AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then
    10.                 'Check for a-z, A-Z and whitespace (\s).
    11.                 'Make sure only upper case, lower case and spaces allowed.
    12.                 'No spaces allowed without the existence of letters.
    13.                 If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then
    14.                     ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    15.                     bCheckFailed = bCheckFailed Or True
    16.                     'No text entered.
    17.                 ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
    18.                     ErrProv.SetError(ctrl, "You must supply a value.")
    19.                     bCheckFailed = bCheckFailed Or True
    20.                 Else
    21.                     ErrProv.SetError(ctrl, "")
    22.                     bCheckFailed = bCheckFailed Or False
    23.                 End If
    24.             Else
    25.                 'Check for a-z, A-Z and whitespace (\s).
    26.                 'Make sure only upper case, lower case and spaces allowed.
    27.                 'No spaces allowed without the existence of letters.
    28.                 If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    29.                     ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    30.                     bCheckFailed = bCheckFailed Or True
    31.                     'No text entered.
    32.                 ElseIf Not CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
    33.                     ErrProv.SetError(ctrl, "You must supply a value.")
    34.                     bCheckFailed = bCheckFailed Or True
    35.                 Else
    36.                     ErrProv.SetError(ctrl, "")
    37.                     bCheckFailed = bCheckFailed Or False
    38.                 End If
    39.             End If
    40.         ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
    41.             'ComboBox is editable.
    42.             If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then
    43.                 'Something is present but not just spaces.
    44.                 If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _
    45.                 AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then
    46.                     'Check for a-z, A-Z and whitespace (\s).
    47.                     'Make sure only upper case, lower case and spaces allowed.
    48.                     'No spaces allowed without the existence of letters.
    49.                     If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    50.                         ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    51.                         bCheckFailed = bCheckFailed Or True
    52.                         'No text entered.
    53.                     ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
    54.                         ErrProv.SetError(ctrl, "You must supply a value.")
    55.                         bCheckFailed = bCheckFailed Or True
    56.                     Else
    57.                         ErrProv.SetError(ctrl, "")
    58.                         bCheckFailed = bCheckFailed Or False
    59.                     End If
    60.                 End If
    61.             End If
    62.         Else
    63.             'Check for a-z, A-Z and whitespace (\s).
    64.             'Make sure only upper case, lower case and spaces allowed.
    65.             'No spaces allowed without the existence of letters.
    66.             If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    67.                 ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    68.                 bCheckFailed = bCheckFailed Or True
    69.                 'No text entered.
    70.             ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
    71.                 ErrProv.SetError(ctrl, "You must supply a value.")
    72.                 bCheckFailed = bCheckFailed Or True
    73.             Else
    74.                 ErrProv.SetError(ctrl, "")
    75.                 bCheckFailed = bCheckFailed Or False
    76.             End If
    77.         End If
    78.     End Sub

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    OK, I tried the following and still not working. Same as prior post.
    vb Code:
    1. Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control)
    2.         bCheckFailed = True
    3.  
    4.         Dim ErrProv As New ErrorProvider
    5.  
    6.         If TypeOf ctrl Is Elegant.Ui.TextBox Then
    7.             'Something is present but not just spaces.
    8.             If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _
    9.             AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then
    10.                 'Check for a-z, A-Z and whitespace (\s).
    11.                 'Make sure only upper case, lower case and spaces allowed.
    12.                 'No spaces allowed without the existence of letters.
    13.                 If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then
    14.                     ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    15.                     bCheckFailed = bCheckFailed Or True
    16.                 Else
    17.                     ErrProv.SetError(ctrl, "")
    18.                     bCheckFailed = bCheckFailed Or False
    19.                 End If
    20.             ElseIf Not CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
    21.                 ErrProv.SetError(ctrl, "You must supply a value.")
    22.                 bCheckFailed = bCheckFailed Or True
    23.             Else
    24.                 ErrProv.SetError(ctrl, "")
    25.                 bCheckFailed = bCheckFailed Or False
    26.             End If
    27.         ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
    28.             'ComboBox is editable.
    29.             If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then
    30.                 'Something is present but not just spaces.
    31.                 If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _
    32.                 AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then
    33.                     'Check for a-z, A-Z and whitespace (\s).
    34.                     'Make sure only upper case, lower case and spaces allowed.
    35.                     'No spaces allowed without the existence of letters.
    36.                     If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
    37.                         ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
    38.                         bCheckFailed = bCheckFailed Or True
    39.                     Else
    40.                         ErrProv.SetError(ctrl, "")
    41.                         bCheckFailed = bCheckFailed Or False
    42.                     End If
    43.                 ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
    44.                     ErrProv.SetError(ctrl, "You must supply a value.")
    45.                     bCheckFailed = bCheckFailed Or True
    46.                 Else
    47.                     ErrProv.SetError(ctrl, "")
    48.                     bCheckFailed = bCheckFailed Or False
    49.                 End If
    50.             End If
    51.         End If
    52.     End Sub

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Best way to accomplish this task

    OK, still not working right. It's allowing numbers when it shouldn't be. I won't spam my thread, so I'll work on the issue and if I have trouble,I'll be back. This won't beat me! :-)

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Best way to accomplish this task

    What I had in mind was something like this:

    Code:
    Function CheckForInput(ByVal strInput As String) As Boolean
        If Input Is Valid 'Perform any regex or other tests/comparisons/checks here
            Return True
        Else
            Return False
        End If
    End Function
    
    'This could be your keypress, lostfocus or any other control event
    Private Sub Control_Event(Sender As Object, E As EventArgs) 
        If TypeOf Sender Is Elegant.UI.Control Then
            If Not CheckForInput(Sender.Text) Then
                'Raise error for invalid input
            End If
        End If
    End Sub
    Now, you can test the CheckForInput function separately to see if it correctly returns True for valid inputs and False for any invalid inputs (you could check it manually or through unit testing apps such as Microsoft's unit testing or nUnit). This way you can finetune the CheckForInput function to suit your exact needs.

    CheckForInput function should take the text to be validated and return a True/False value based on the validation results. It should not be knowing which control types to check and what to do if the text is not validated. This is part of UI interaction and should be handled in the respective control events.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width