Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ensuring ONLY letters are allowed to be entered into a text box

Posted on 2005-03-22
24
Medium Priority
?
375 Views
Last Modified: 2012-05-05
to ensure that text entered into a text box was letters only i have been given this:

If IsNumeric(txtTitle.Text) = True Then
'Code to go here when the data entered in textbox is numeric
MsgBox "Please enter a title !"
txtTitle.Text = ""
txtTitle.SetFocus
Else
'Code to go here when it is NOT numerical ie an error message box, you can leave this part blank because obviously
'it is letters and so you do not need to do anything

however this code only works if the user enters all numbers as a job title such as 333etc

if i enter head barma2n then the code allows this. I want ONLY letters to be allowed.

any1 help?

End If
0
Comment
Question by:scm0sml
  • 12
  • 6
  • 5
  • +1
24 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 13600369
Hi scm0sml,

Use the KeyPress event:

Private Sub txtTitle_KeyPress(KeyAscii As Integer)
If Instr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",UCase(Chr(KeyAscii))) > 0 Then
  'This is OK
Else
  KeyAscii = 0
  'It isn't a letter so set KeyAscii to 0 to ignore that keypress
End If
End Sub

This will determine whether you have pressed a letter key or not, if not then that keypress is voided. This prevents you from entering in any character not in the list at source.

Tim Cottee
0
 
LVL 1

Assisted Solution

by:gpaulish
gpaulish earned 225 total points
ID: 13600533
Being that you asked to ignore only numerics and not other character symbols this logic may make more sense.  It will allow all characters except numerics, without ignoring potentially important characters like spaces.  

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
End Sub

-GP
0
 

Author Comment

by:scm0sml
ID: 13600679
ive inserted all of these procedures into my program, one at a time obviously and none of them r having any effect?

am i doing sumthing wrong?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:scm0sml
ID: 13603788
hello? any1?

neither procedure is stopping invalid input
0
 
LVL 23

Accepted Solution

by:
gecko_au2003 earned 225 total points
ID: 13604577
You could go through the string using a for next loop and check each value against the IsNumeric function and if it is to not allow that string or at least not that numeric value, do this on the lost focus.

I will check this in VBA so I can figure something out for you but that is what I think you are after :)
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13605022
Your best bet thinking about it would to be either to make your own text control to not allow numerical values to be entered what so ever or make your own function to check what has been entered and get rid of numerical values
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13605102
I found this , this might give you some idea of how to do it with regards to functions :

http://www.taltech.com/resources/intro_to_bc/UPC.vb
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13605125
There is the masked edit control you can format it to only allow text, thinking of that there is also something simmiliar for text fields to only allow data entry of textual type:

http://www.vbexplorer.com/VBExplorer/viewcode.asp?SendText=vb_faq/faq_090598_ht
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13605162
You could also set the max length of the textbox to a certain amount
0
 
LVL 1

Expert Comment

by:gpaulish
ID: 13605362
There is not reason to offer any other solutions than the one I posted, it certainly does work correct.

Keep this in mind, this sub is depended upon your control name, for example my textbox was called
Text1, hence the Sub Text1_KeyPress, if you named your textbox anything other than Text1, then of
course this will not work, so change the name of the sub, or textbox to match.

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
End Sub



-GP
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13605402
http://www.codeproject.com/useritems/Complete_Textbox.asp

Also I think I found the names of the propertys :

DisplayFormat or CustomFormat, those ones allow you to format a textbox and that should only allow data entry of text if you set it to text if that does not work you can also use the Format Function along with it

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctFormat.asp

Where you can Create your own user-defined string formats.

Other then that, ~GP's suggestion was good :)
0
 

Author Comment

by:scm0sml
ID: 13606065
sorry lads but......

ive got exactly this:
Private Sub txtTitle_KeyPress(ByVal KeyAscii As Integer)
        If (KeyAscii >= 48 And KeyAscii <= 57) Then
            KeyAscii = 0
        End If
    End Sub

with my text box obviously being called txtTitle and nothing happens when i type into the box, as in any entry is allowed.

any thoughts?

sorry to be a pain but this looks by far the simplest way to do it if it would work!!
0
 

Author Comment

by:scm0sml
ID: 13606166
the problem is with the KeyPress method. i have tried changing the text of a label and its not working.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13606388
What about using the suggestions I made ?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607095
scm0sml, HERE IS THE PERFECT EXAMPLE FOR YOU :

http://www.thescarms.com/vbasic/SubClassTextBox.asp

go download that, you will have to alter the code so it lets you enter regular text as it converts it to upper case but that shouldnt be  a problem :)
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607126
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607225
Instead of using the keyascii codes 48 and 57, what are the constants ?
0
 
LVL 1

Expert Comment

by:gpaulish
ID: 13607236
are you using visual basic 6.0?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607341
yes he is :)
0
 
LVL 1

Expert Comment

by:gpaulish
ID: 13608531
Then for it not to work makes little sense.  Have you placed a break point in the sub to see if that event was being fired?????
0
 

Author Comment

by:scm0sml
ID: 13609583
im using vb.net lads, think thatmay have been where our prob is??
0
 

Author Comment

by:scm0sml
ID: 13609858
finally got it working with this but ive split the points between th 2 of u as u both put a fair bit into it!!
cheers!

Private Sub TitleValidity()
        Dim validString As String = "ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwxyz ,;.'"
        Dim i, j As Integer
        Dim charvalid As Boolean

        For i = 0 To txtTitle.Text.Length - 1
            For j = 0 To validString.Length - 1
                If txtTitle.Text.Chars(i) = validString.Chars(j) Then
                    charvalid = True
                End If
            Next
            If j = validString.Length And charvalid = False Then
                MsgBox("invalid title")
            End If
        Next
    End Sub
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13609873
Thank you for the points and grade, glad you got your problem solved !
0
 
LVL 1

Expert Comment

by:gpaulish
ID: 13612992
ohh yes vb.net would be a problem as events are handled differently, thanks for all.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question