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

Validation on a text control 2

Status
Not open for further replies.

MichaelintheUK

Programmer
May 24, 2003
88
0
0
GB
Hello

I have a text box on a form into which is entered a 7 or 8 character id number. In most cases this is all numbers - however because of amalgamations with other sites they have id numbers with a prefix of A or B followed by the rest being digits. I have managed to provide checks for the length and restricted the keypress to only accept the following:

0123456789AB or delete or backspace.

However I want to allow the A or B or a digit only as the first character and then only allow digits for the rest of the id number.

This is a problem when editing because at the keypress stage the edited key position is not known?

Is this possible using the keypress or is there another way to differ the validation rules between the first character and the rest of the characters?

thanks

Michael
 
If you have to validate as they type then you can use the .SelStart property of the textbox to see where the cursor is.

I'm personally not a bit fan of validating while typing though, you could restrict the values they can enter and then validate later (when leaving the textbox, when changes are submitted etc.).

Hope this helps



HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I think I'd konsider keeping the event as is, and do an additional validation in the controls before update event.

It could look something like this

[tt]If TheControl like "[AB0-9]#######" Or TheControl like "[AB0-9]######" Then
MsgBox "It's OK"
Else
MsgBox "tell what's wrong"
Cancel = True
End If[/tt]

Roy-Vidar
 
If you want to stick with keypress, here is another possibility, though not as neat as RoyVidar's solution.

Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)

If Len(Text0.Text & "") > 8 Then
    KeyAscii = 0
Else
    If Len(Text0.Text & "") > 0 Then
        If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8) Then
            KeyAscii = 0
        End If
    Else
        If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii < 65 Or KeyAscii > 66) And (KeyAscii <> 8) Then
            KeyAscii = 0
        End If
    End If
End If
End Sub

 
I'd modify that in a couple of ways (to allow for backspace when 8 chars are present and allow for users inputting a valid A or B to start the string at any time when there aren't 8 chars present):
Code:
If Len(Text0.Text & "") > 8 [red]And KeyAscii <> 8[/red] Then
    KeyAscii = 0
Else
    If Len(Text0.Text & "") > 0 [red]And Text0.SelStart > 0[/red] Then
        If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii <> 8) Then
            KeyAscii = 0
        End If
    Else
        If (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii < 65 Or KeyAscii > 66) And (KeyAscii <> 8) Then
            KeyAscii = 0
        End If
    End If
End If
Cheers

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
HarleyQuin

Thanks for your reply - You first posting has made me think again about my approach regarding the method of validating the text box entry.

Thanks also Roy-Vidar and eveyone else that posted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top