Reputation: 3
I'm having a bit of trouble trying to think of ways to validate this textbox so that it will prevent users from entering numbers or special symbols. I want it so that, upon clicking the "Save" Button (btnSaveJob), it will validate the textbox for the Name. The textbox is for entering names, so I'd like to try and validate it. Here is the code:
Private Sub btnSaveJob_Click(sender As Object, e As EventArgs) Handles btnSaveJob.Click
Dim txtName As TextBox
If String.IsNullOrEmpty(txtName.Text) Then
End If
Try
Dim result As DialogResult
result = MessageBox.Show("Do you want to save the selected job?", "Save Job",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If (result = DialogResult.Yes) Then
Validate()
TimetableBindingSource.EndEdit()
TableAdapterManager.UpdateAll(Me.JobTimetableDataSet)
MessageBox.Show("The Job has been saved successfully.", "Save Job", MessageBoxButtons.OK, MessageBoxIcon.Information)
RefreshData()
btnAddJob.Text = "Add New Job"
Else
'Exit Sub
Return
End If
Catch ex As Exception
MessageBox.Show("Save Job Failed: " & ex.Message.ToString(),
"Save Job", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Upvotes: 0
Views: 43
Reputation: 54417
To answer the question as asked:
If txtName.Text.Any(Function(ch) Char.IsNumber(ch) OrElse Char.IsSymbol(ch)) Then
'The text is invalid.
End If
That checkes whether any character is a number or a symbol. That may not be exactly what you want though. You can adjust the Boolean
expression however you like, while the principle remains the same, e.g.
If txtName.Text.All(Function(ch) Char.IsLetter(ch) OrElse {" "c, "'"c}.Contains(ch)) Then
'The text is valid.
End If
That last one checks that all characters are either letters, spaces or apostrophes. You can add whatever combination of conditions you want.
Upvotes: 1