Verifying Password

ziyad

New member
Joined
Jul 18, 2005
Messages
1
Programming Experience
Beginner
Please need help to verify that password contains only alphabet characters nothing else thank you
 
Welcome ziyad. This and similar variations are popular questions on this and other forums. To give you a quicker answer in future, may I suggest a quick keyword search of the forums before posting in future.

You can use this code to prevent the user typing an non-alphabetic characters into the TextBox:
VB.NET:
	Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsLetter(e.KeyChar) Then
			'Do not accept the character.
			e.Handled = True

			'Alert the user that the character has been rejected.
			Beep()
		End If
	End Sub
The only problem with this is that it doesn't prevent the user from pasting invalid characters from the clipboard. This code will handle pasting using Ctrl+V:
VB.NET:
	Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		If Convert.ToInt32(e.KeyChar) = 22 Then
			'The user pressed Ctrl+V.
			Dim cbData As IDataObject = Clipboard.GetDataObject

			'Check whether the clipboard contains text.
			If cbData.GetDataPresent(DataFormats.Text) Then
			    'The clipboard contains text that can be pasted.
			 Dim cbString As String = CType(cbData.GetData(DataFormats.Text), String)

			    'Check that every character is a letter.
			    For i As Integer = 0 To cbString.Length - 1 Step 1
				 If Not Char.IsLetter(cbString.Chars(i)) Then
					 'Do not accept the clipboard data.
					 e.Handled = True

					 'Alert the user that the character has been rejected.
					 Beep()

					 Return
					End If
				Next
			End If
		ElseIf Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsLetter(e.KeyChar) Then
			'Do not accept the character.
			e.Handled = True

			'Alert the user that the character has been rejected.
			Beep()
		End If
	End Sub
You may also want to just reject any pasting, which is not bad idea for a masked password. This still does not prevent the user from right-clicking and selecting Paste, though. This is a bit of a hack to prevent this, but I'm not aware of a better way. Add a ContextMenu to your form but do not add any MenuItems to it. In the Properties window, assign this ContextMenu to the ContextMenu property of your TextBox. Now the user will not get the standard popup menu when they right click so they can't paste that way.
 
I just put "isalpha" into a help search and got nothing. I also looked in the Microsoft.VisualBasic.Strings module and the System.String structure and there were no members that looked like they would do that job.

With regards to the code I posted, rather than iterating over the Chars property of the string, it may be more efficient to call ToCharArray and iterate over the elements. It would have to be a large string to notice any difference though.
 
Back
Top