Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensure input to a text field begins with a particular letter?

Status
Not open for further replies.

rundmc

Programmer
Jan 16, 2003
15
US
I need a way to validate entries into a text box ensuring that each entry begins with P can anyone help me??
 
You can use the left function to determine if the first letter is a 'P'. You can check this in the validate ot lost focu event.

if left(text1,1)="P" then...

Add the UCase function if it is not case sensitive.


if Ucase(left(text1,1))="P" then...

Another method would be to seed the text box with a "P" before the user eneters anything. Or use the keypress event to only allow the first letter to be a "P".

if len(text1)=0 then
if not keyascii=<value> then keyascii=0
end if

Where <value> is the ascii value for the letteer 'P'. You will need to look that up in the ascii character set.
Thanks and Good Luck!

zemp
 

in the keydown event

if left(textbox.text,1)<>P then
'do what you want - message box or just discard the keypress
end if

 
Will it matter whether it's a lower-case &quot;p&quot; or is it
okay to always have it an uppercase &quot;P&quot;?
Alternatively, you can add code to ALWAYS add a &quot;P&quot; to
whatever the user writes in the text box.
Also, you may check out input boxes. They're similar to
text boxes.
 
This will work for what you needed:


Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Text1.Text) < 1 Then
If KeyAscii <> 80 And KeyAscii <> 112 Then
KeyAscii = 0
End If
End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top