|
-
Jan 22nd, 2011, 07:50 PM
#1
Thread Starter
PowerPoster
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:
<CLSCompliant(True)> Public Sub WhiteSpaceAndLettersOnly(ByVal ctrl As Elegant.Ui.Control)
'This is going to be used on TextBoxes and editable ComboBoxes.
'This will not be used on txtWorkLog.
'Allow whitespace and letters only.
'Do not allow only whitespace without letters existing.
If TypeOf ctrl Is Elegant.Ui.TextBox Then 'TextBox.
If Not ctrl.Name Is "txtWorkLog" Then 'Not txtWorkLog.
If Not String.IsNullOrEmpty(ctrl.Text) Then 'Isnt empty.
End If
ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
If DirectCast(ctrl, Elegant.Ui.ComboBox).Editable = True Then
End If
End If
End If
End Sub
-
Jan 22nd, 2011, 10:09 PM
#2
Thread Starter
PowerPoster
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:
<CLSCompliant(True)> Private Sub txtName_TextChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles txtName.TextChanged 'Make sure the name is auto titlecased. TitleCaseTextBox(txtName) Dim ErrProv As New ErrorProvider 'Make sure only upper case, lower case and spaces allowed. 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
-
Jan 23rd, 2011, 11:08 AM
#3
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.
.
-
Jan 23rd, 2011, 11:47 AM
#4
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
-
Jan 23rd, 2011, 05:59 PM
#5
Thread Starter
PowerPoster
Re: Best way to accomplish this task
 Originally Posted by kevininstructor
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:
Public Function WhitespaceLettersOnly(ByVal ctrl As Elegant.Ui.Control) As Boolean WhitespaceLettersOnly = False Dim ErrProv As New ErrorProvider 'Make sure only upper case, lower case and spaces allowed. 'No spaces allowed without the existence of letters. If TypeOf ctrl Is Elegant.Ui.TextBox Then If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") WhitespaceLettersOnly = WhitespaceLettersOnly Or True ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") WhitespaceLettersOnly = WhitespaceLettersOnly Or True Else ErrProv.SetError(ctrl, "") WhitespaceLettersOnly = WhitespaceLettersOnly Or False End If End If ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") WhitespaceLettersOnly = WhitespaceLettersOnly Or True ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") WhitespaceLettersOnly = WhitespaceLettersOnly Or True Else ErrProv.SetError(ctrl, "") WhitespaceLettersOnly = WhitespaceLettersOnly Or False End If End If End If End If Return WhitespaceLettersOnly End Function
Last edited by BrailleSchool; Jan 23rd, 2011 at 06:18 PM.
Reason: Updated code.
-
Jan 23rd, 2011, 06:19 PM
#6
Thread Starter
PowerPoster
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.
-
Jan 23rd, 2011, 06:26 PM
#7
Thread Starter
PowerPoster
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:
Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control) bCheckFailed = True Dim ErrProv As New ErrorProvider If TypeOf ctrl Is Elegant.Ui.TextBox Then 'Something is present but not just spaces. If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then 'Check for a-z, A-Z and whitespace (\s). 'Make sure only upper case, lower case and spaces allowed. 'No spaces allowed without the existence of letters. If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") bCheckFailed = bCheckFailed Or True 'No text entered. ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If End If ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then 'ComboBox is editable. If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then 'Something is present but not just spaces. If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then 'Check for a-z, A-Z and whitespace (\s). 'Make sure only upper case, lower case and spaces allowed. 'No spaces allowed without the existence of letters. If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") bCheckFailed = bCheckFailed Or True 'No text entered. ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If End If End If End If End Sub
Last edited by BrailleSchool; Jan 23rd, 2011 at 06:40 PM.
-
Jan 23rd, 2011, 06:48 PM
#8
Thread Starter
PowerPoster
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:
Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control)
bCheckFailed = True
Dim ErrProv As New ErrorProvider
If TypeOf ctrl Is Elegant.Ui.TextBox Then
'Something is present but not just spaces.
If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _
AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then
'Check for a-z, A-Z and whitespace (\s).
'Make sure only upper case, lower case and spaces allowed.
'No spaces allowed without the existence of letters.
If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then
ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
bCheckFailed = bCheckFailed Or True
'No text entered.
ElseIf CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
ErrProv.SetError(ctrl, "You must supply a value.")
bCheckFailed = bCheckFailed Or True
Else
ErrProv.SetError(ctrl, "")
bCheckFailed = bCheckFailed Or False
End If
Else
'Check for a-z, A-Z and whitespace (\s).
'Make sure only upper case, lower case and spaces allowed.
'No spaces allowed without the existence of letters.
If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
bCheckFailed = bCheckFailed Or True
'No text entered.
ElseIf Not CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then
ErrProv.SetError(ctrl, "You must supply a value.")
bCheckFailed = bCheckFailed Or True
Else
ErrProv.SetError(ctrl, "")
bCheckFailed = bCheckFailed Or False
End If
End If
ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then
'ComboBox is editable.
If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then
'Something is present but not just spaces.
If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _
AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then
'Check for a-z, A-Z and whitespace (\s).
'Make sure only upper case, lower case and spaces allowed.
'No spaces allowed without the existence of letters.
If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
bCheckFailed = bCheckFailed Or True
'No text entered.
ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
ErrProv.SetError(ctrl, "You must supply a value.")
bCheckFailed = bCheckFailed Or True
Else
ErrProv.SetError(ctrl, "")
bCheckFailed = bCheckFailed Or False
End If
End If
End If
Else
'Check for a-z, A-Z and whitespace (\s).
'Make sure only upper case, lower case and spaces allowed.
'No spaces allowed without the existence of letters.
If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then
ErrProv.SetError(ctrl, "Value may only contain letters and spaces.")
bCheckFailed = bCheckFailed Or True
'No text entered.
ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then
ErrProv.SetError(ctrl, "You must supply a value.")
bCheckFailed = bCheckFailed Or True
Else
ErrProv.SetError(ctrl, "")
bCheckFailed = bCheckFailed Or False
End If
End If
End Sub
-
Jan 23rd, 2011, 06:54 PM
#9
Thread Starter
PowerPoster
Re: Best way to accomplish this task
OK, I tried the following and still not working. Same as prior post.
vb Code:
Public Sub WhiteSpaceLettersOnly(ByVal ctrl As Elegant.Ui.Control) bCheckFailed = True Dim ErrProv As New ErrorProvider If TypeOf ctrl Is Elegant.Ui.TextBox Then 'Something is present but not just spaces. If CType(ctrl, Elegant.Ui.TextBox).TextLength > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.TextBox).TextLength.ToString IsNot Nothing Then 'Check for a-z, A-Z and whitespace (\s). 'Make sure only upper case, lower case and spaces allowed. 'No spaces allowed without the existence of letters. If Not New Regex("^[azA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If ElseIf Not CType(ctrl, Elegant.Ui.TextBox).TextLength = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If ElseIf TypeOf ctrl Is Elegant.Ui.ComboBox Then 'ComboBox is editable. If CType(ctrl, Elegant.Ui.ComboBox).Editable = True Then 'Something is present but not just spaces. If CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length > 0 _ AndAlso Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length.ToString IsNot Nothing Then 'Check for a-z, A-Z and whitespace (\s). 'Make sure only upper case, lower case and spaces allowed. 'No spaces allowed without the existence of letters. If Not New Regex("^[a-zA-Z\s]*$").IsMatch(ctrl.Text) Then ErrProv.SetError(ctrl, "Value may only contain letters and spaces.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If ElseIf Not CType(ctrl, Elegant.Ui.ComboBox).Text.Trim.Length = 0 Then ErrProv.SetError(ctrl, "You must supply a value.") bCheckFailed = bCheckFailed Or True Else ErrProv.SetError(ctrl, "") bCheckFailed = bCheckFailed Or False End If End If End If End Sub
Last edited by BrailleSchool; Jan 23rd, 2011 at 07:01 PM.
-
Jan 23rd, 2011, 07:06 PM
#10
Thread Starter
PowerPoster
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! :-)
-
Jan 23rd, 2011, 10:31 PM
#11
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.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|