How to determine if there are at least two characters entered in a text box? 
Author Message
 How to determine if there are at least two characters entered in a text box?

I have a text box that the user must enter at least two characters. How do I
validate this?

EXTRA CREDIT:
How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
textbox, regardless of length?

Thanks in advance...
Steve

--
Steve Reich
WarePro System Software
http://www.*-*-*.com/

ICQ#: 14665598



Sun, 22 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
If you wish to find out if two characters are entered in a text box then
place this code whereever it is being checked.

if len(text1.text) > 2 then
    ... code if their is more than 2 characters entered.

Thank You
Farshad Hemmati

Quote:

>I have a text box that the user must enter at least two characters. How do
I
>validate this?

>Thanks in advance...
>Steve

>--
>Steve Reich
>WarePro System Software
>http://www.warepro.com

>ICQ#: 14665598



Sun, 22 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
The following code will help you out with the validation

Private Sub txTextBox_Validate(Cancel As Boolean)
    If Len(txTextBox.Text) < 2 Then
        MsgBox "Must enter 2 or more letters", , "Validation Error"
        Cancel = True

    End If

End Sub

As for checking the alphanumericness (is this a word?) of the entered text,
I think you'd need to check each individual characters' ASCII code, via the
Asc function. I can't tell you offhand the range of codes, but ACSII charts
are littered all through the VS documentation...

There is probably a better way than this, but I can't imagine that this
would be a big performance hit, unless you were doing it on a huge field...

John Townes
Sydney, Australia

Quote:

>I have a text box that the user must enter at least two characters. How do
I
>validate this?

>EXTRA CREDIT:
>How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
>textbox, regardless of length?

>Thanks in advance...
>Steve

>--
>Steve Reich
>WarePro System Software
>http://www.warepro.com

>ICQ#: 14665598



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
I am not sure if I understand what you are looking for, But it seems
like you would like to know if 2 charactors are entered in a text box.

If this is the case, cant you use the (char) command and see if the
position holds a value or a null (" ") ??
the last part of your question is exactly that. Find the value of 0
and 9. you will see that the values increase by one (respectfully)
then do a simple "if" statement.
if textbox.txt = char(Whatever 0 is) to char(whatever 9 is) then
this will work for a-z also
On Wed, 4 Nov 1998 20:30:46 -0500, "Steve Reich"

Quote:

>I have a text box that the user must enter at least two characters. How do I
>validate this?

>EXTRA CREDIT:
>How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
>textbox, regardless of length?

>Thanks in advance...
>Steve

>--
>Steve Reich
>WarePro System Software
>http://www.warepro.com

>ICQ#: 14665598



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
To answer the second part of your question.

Instead of checking whether a user has entered something you do not want
them to, why not disallow them to do so by using the KeyPress event: -

        Private Sub txtInput_KeyPress(KeyAscii As Integer)

Code this sort of thing: -

        Select Case KeyAscii
        Case 0 To 7, 9 To 35, 37 To 64, 91 To 96, 123 To 162, 164 To 255
        KeyAscii = 0
        End Select

Search ascii in help and it gives you all the codes.

I got this way of coding from this newsgroup yesterday and it works fine.

Pete Boys
London

Quote:

>I have a text box that the user must enter at least two characters. How do
I
>validate this?

>EXTRA CREDIT:
>How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
>textbox, regardless of length?

>Thanks in advance...
>Steve

>--
>Steve Reich
>WarePro System Software
>http://www.warepro.com

>ICQ#: 14665598



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
If Len(text1.text) = 2 then
        'length is 2
end if

-or-

if len(trim(text1.text)) = 2 then
        'length is 2 (with any empty space trimmed off)
end if

EXTRA CREDIT

ASCII codes for number (0-9) are 48 - 57.
ASCII codes for capital letters (A-Z) are 65 - 90.

Loop through each character in the text box (use the Mid$() function) and
check the ascii code of each character (Asc() function).  There may be an
easier way to do this but it will definitely work.



Quote:
> I have a text box that the user must enter at least two characters. How
do I
> validate this?

> EXTRA CREDIT:
> How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
> textbox, regardless of length?

> Thanks in advance...
> Steve

> --
> Steve Reich
> WarePro System Software
> http://www.warepro.com

> ICQ#: 14665598



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?

Quote:

>To answer the second part of your question.

>Instead of checking whether a user has entered something you do not want
>them to, why not disallow them to do so by using the KeyPress event: -

>        Private Sub txtInput_KeyPress(KeyAscii As Integer)

>Code this sort of thing: -

>        Select Case KeyAscii
>        Case 0 To 7, 9 To 35, 37 To 64, 91 To 96, 123 To 162, 164 To 255
>        KeyAscii = 0
>        End Select

The one problem in this approach is that is does not prevent the user from
PASTING text into the box. (I admit that I use this method, so my
"validated" text boxes are susceptible to this "back door" as well.) The
recommend solution is to use the Change event of the text box. I personally
opt for a combination of the two. Perhaps someday I'll sit down and see if
the text box can be subclassed to prevent pasting. (Probably all that needs
to be done is to trap and ignore WM_PASTE.) Then again, that wouldn't be
very Windows-like, would it?

And although someone said he could prevent the user from using Ctrl-V to
paste by trapping Chr$(22), this doesn't work with Shift-Insert.

Bottom line: Change event.



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
I agree with your answer but if I read correctly, the question said at least
two characters...therefore instead of Len(text1.text) = 2, it should probably
read If Len(text1.text) >= 2.....

Quote:

> If Len(text1.text) = 2 then
>         'length is 2
> end if

> -or-

> if len(trim(text1.text)) = 2 then
>         'length is 2 (with any empty space trimmed off)
> end if

> EXTRA CREDIT

> ASCII codes for number (0-9) are 48 - 57.
> ASCII codes for capital letters (A-Z) are 65 - 90.

> Loop through each character in the text box (use the Mid$() function) and
> check the ascii code of each character (Asc() function).  There may be an
> easier way to do this but it will definitely work.

Also instead of looping through the string entered...He can use the keypress
event to check for the ASCII values of entered characters as he enters them,
generating an error when he enters the wrong type (numeric vs. alpha).


Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
Jeff:
I like the Change Event approach also because it seems to cover all types of
input.  Another approach that works and requires no key by key or paste by
paste  validation is to use a command button (an OK button) on your form
that when clicked validates all of the data entered on the form.  If it
finds something it doesn't like, it displays a message box and returns focus
to the offending control.  This is an efficient way to do it but limits your
flexibility somewhat.  There will always be situations where you need to
validate as data is entered.  One example is to use data entered at the
beginning of a form to determine default values for controls further along
on the form.  The Change Event seems to be the way to handle that type of
situation.  I look forward to using VB6 with the new Validation Event.  It
might solve all these problems.
John
Quote:


>>To answer the second part of your question.

>>Instead of checking whether a user has entered something you do not want
>>them to, why not disallow them to do so by using the KeyPress event: -

>>        Private Sub txtInput_KeyPress(KeyAscii As Integer)

>>Code this sort of thing: -

>>        Select Case KeyAscii
>>        Case 0 To 7, 9 To 35, 37 To 64, 91 To 96, 123 To 162, 164 To 255
>>        KeyAscii = 0
>>        End Select

>The one problem in this approach is that is does not prevent the user from
>PASTING text into the box. (I admit that I use this method, so my
>"validated" text boxes are susceptible to this "back door" as well.) The
>recommend solution is to use the Change event of the text box. I personally
>opt for a combination of the two. Perhaps someday I'll sit down and see if
>the text box can be subclassed to prevent pasting. (Probably all that needs
>to be done is to trap and ignore WM_PASTE.) Then again, that wouldn't be
>very Windows-like, would it?

>And although someone said he could prevent the user from using Ctrl-V to
>paste by trapping Chr$(22), this doesn't work with Shift-Insert.

>Bottom line: Change event.



Mon, 23 Apr 2001 03:00:00 GMT  
 How to determine if there are at least two characters entered in a text box?
Add your code in the Keypressed sub-routine

sub Keypressed( ......)

dim sNum as string
dim validate as string

        validate = "0123456789"

        sNum = Char$(keypressed)

        if instr(sNum, validate) then
                msgbox "Numeric"
        else
                msgbox "Non-Numeric"
        endif

end sub

IsNumeric function is another option.



Quote:
> I have a text box that the user must enter at least two characters. How
do I
> validate this?

> EXTRA CREDIT:
> How do I tell if only numbers (0-9) or letters (A-Z) are entered in the
> textbox, regardless of length?

> Thanks in advance...
> Steve

> --
> Steve Reich
> WarePro System Software
> http://www.warepro.com

> ICQ#: 14665598



Tue, 24 Apr 2001 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. How to determine if there are at least two characters entered in a text box?

2. enter two columns of text to a text or edit box

3. Entering chinese character in rich text box.

4. Tabbing from text box to text box using the enter key

5. enter ctrl+enter key into a text box

6. text entered into Text box increments a variable

7. Keeping focus on button while entering text in text box

8. Entering text into a text box at a specific point

9. Keeping focus on button while entering text in text box

10. Validating text entered in a text box??????

11. Text from vba text box (>2000 characters) to a Word bookmard

12. determining if two Picture Boxes are overlapping

 

 
Powered by phpBB® Forum Software