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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text boxes validation

Status
Not open for further replies.

angelpr23

Programmer
Mar 15, 2007
37
GR
hello everybody.
I want to create a kind of validation in some textboxes i use in form.

For TxtPhone i want the user allowed to type only 10 numeric characters for example 2105839489 or 6939583952 no alphabetical characters and no panctuation marks

for TxtPostCode i want the user allowed to type only 5 numeric characters like "13463" only numeric not alphabetical characters too and no punctuation marks

For TxtPrice i want the user to have the possibility to type only numeric characters 1346.64 represents currency not punction marks and alphabetical caractes.

Any ideas how to do this?
Thank you all in advanced.
 
Consider this code:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
    Case 48 To 57, 8
        [green]' 0 to 9, plus 8 - Backspace[/green]
    Case Else
        KeyAscii = 0
End Select

End Sub

As far as number of characters, look up Len() function.

Have fun.

---- Andy
 
Thank you very much, that was great for all my requirements.
 
Maximum number of characters in a TextBox is controlled with the MaxLength Properties.

[gray]Experience is something you don't get until just after you need it.[/gray]
 

Good point, Error7

The Len() function may be usefull to check if the Phone or Postal Code is filled in full, or are there any digits missing: 212555121 has one digit missing from the phone number and that could be detected in the code and user may have to fix it before garbage goes into database.

Have fun.

---- Andy
 
Good point Andy.

In the U.K. a phone number can be anything from 6 to at least 11 digits so the Len() function wouldn't be much use.

There again, our Post Codes are always alpha numeric.

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
You could always use the Microsoft Masked Edit Control instead of a text box.

Swi
 
Thank all of you so much,

for postcode for example i tried this:

Private Sub postcode_KeyPress(keyascii As Integer)
Dim cnt As Integer
cnt = 0
Select Case keyascii
Case 48 To 57, 8
' 0 to 9, plus 8 - Backspace
Case Else
keyascii = 0
Exit Sub
End Select
If Len(postcode.Text) = 5 Then
If keyascii >= 0 And keyascii <= 255 Then
keyascii = 0
End If
End If
end sub

Andy I am wandering what is the best way, to use len() function or maxlength property?
Swi what exactly does mask edit control? I am new in vb6 and i don't know many things.
 
What's this used for: Dim cnt As Integer

Select Case keyascii
Case 48 To 57, 8
' 0 to 9, plus 8 - Backspace
Case Else
keyascii = 0
Exit Sub
End Select

You don't need this:-
If Len(postcode.Text) = 5 Then
If keyascii >= 0 And keyascii <= 255 Then
keyascii = 0
End If
End If
end sub

Just set the MaxLength to 5.


[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thank you much error, really i don't need cnt variable.
I have a question to do.
For Pricetxt i refered before i tryied:

Private Sub Pricetxt_KeyPress(keyascii As Integer)
dim dotCnt as integer
'dotCnt represents the number of how many times the user
'has the possibility to type '.' which is usefull for the
'split between integral and fractional part and is one
'because we talk about currency number.

If keyascii = 46 Then
dotCnt = dotCnt + 1
End If
If dotCnt <= 1 Then
Select Case keyascii
Case 48 To 57, 8
' 0 to 9, plus 8 - Backspace
Case Else
If keyascii <> 46 Then
keyascii = 0
Exit Sub
End If
End Select
Else
keyascii = 0
End If
End Sub

About currency type in vb6 i know that the integral size is 15 digits and the fractional is 4 digits. Well, if i set dataformat property of pricetxt to currency (123.45) no negative numbers, the user can type to pricetxt 8 digits for integral size and as much as he wants for fractional. As a conclusion, I want the user has the possibility to type currency numbers in the pricetxt textbox not other characters (punctuation marks and alphabetical characters) for some future calculations in my program. Any help will be much appreciated.

Thank you
all again
in advanced
 


angelpr23,

Over the past three months you have made TEN posts in which you have asked numerous question and have recieved many good tips.

Yet, you have NEVER responded, to
[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].

The Stars accomplish several important things.

First, it gives positive feedback to contributors, that their posts have been helpful.

Second, it identifies threads as containing helpful posts, so that other members can benefit.

And third, it identifies YOU, the original poster (OP), as a greatful member, that not only receives, but is willing to give tokens of thanks.

Skip,

[glasses] [red][/red]
[tongue]
 
In your Project menu go to Components or (ctrl + t) and select the Microsoft Masked Edit Contol. It will then be available for you to place on your form.

Swi
 
SkipVought i have saw some posts i havent responded because i had problem with my internet connection and i had then i had forgotten this. I found 2 or 3 posts i dont remember something else? Could you tell me the post you re mentioned to have a look at it?
 
Thank you Swi, does it have the same functionallity with textbox? Do you have a tutorial link about maskedit, or a link in the website or whatever? I didn't found really helpful msdn help maskedit control in vb6.
Any help for PriceTxt i mentioned before? Does anybody knows? I haven't found yet a solution about this.
 
<Could you tell me the post you re mentioned to have a look at it?

If you're asking if Skip could tell you which post HE helped you on, so you can give him a star, well no he can't. That would be too close to soliciting stars, which is VERY bad etiquette. :) On the other hand, next time someone is of particular help to you, just give them a star, for the reasons Skip explained.

<I didn't found really helpful msdn help maskedit control in vb6.

Well, there's a reason for that. It's because the masked edit control just isn't really helpful to begin with. :p I never use it.

Bob
 
Catching the keystrokes wouldn't help angelpr23 if the user cut and pasted a value into the textbox. He would have to catch that seperately. I think that there was a thread about that a while ago.

Swi
 
Capturing the keystrokes and using the Edit control's Validation event are often sufficient enough for validation.

If you want to lock this down even tighter, then try using the Edit box's Change event to validate input, along with the Windows Undo feature (SendMessage) for the Edit box in order to reverse invalid input.
 
angelpr23
<Could you tell me the post you re mentioned to have a look at it?
Just click on your username at the start of any post you have made and you will see a list of all your stats.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top