-
How to restrict users to enter first character as alphabet and rest as numeric.
Hi! I'm new in VB6. I would like to know how to make a function that would restrict users to enter a 4 character code and the first character is any of the ALPHABET (A~Z) then the last 3 characters would be NUMERIC (0~9). It must not accept no other symbols and they cannot leave it blank (meaning null). :blush:
Example:
Valid code is C105 --> since first character is an alphabet and the rest is numeric
Invalid codes are 9C32
C+54
null
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Use the Like operator
Example:
VB Code:
Dim valid_entry As Boollean
valid_entry = Text1.Text Like "[A-Z]###"
valid_entry will be True if the text in the textbox is correct
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Private Sub txtEntry_Validate
' place this function in the validate event
if Validate(txtEntry)=True then
' great ... go on
else
' post and error message here
' don't let them out
txtentry.setfocus
exit sub
end if
end sub
Private Function ValidateNumber(ByVal TheString as string) as boolean
dim x as integer
if len(TheString)<>4 then
ValidateNumber=False
exit function
else
if isDigit(mid(TheString,x,1))=True then
' the first char is indeed a digit... now check for alpha for 2-4 chars
for x=2 to 4
is isalpha(thestring,x,1)=True then
else
ValidateNumber=False
exit function
end if
next x
end if
' must be good entry
ValidateNumber=True
exit function
end if
end function
========================================
' i didn't check the syntax but you'll see the idea.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Welcome to the forums ! (by the way :))
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
' sorry i forgot to add these as well
Function IsDigit(InputChar) As Integer
' is it 0-9 or a minus char
If (InputChar >= "0" And InputChar <= "9") Or InputChar = "-" Then
IsDigit = True
Else
IsDigit = False
End If
End Function
Function IsPureDigit(InputChar) As Integer
' is it 0-9 or a minus char
If (InputChar >= "0" And InputChar <= "9") Then
IsPureDigit = True
Else
IsPureDigit = False
End If
End Function
Function IsAlpha(Character As String) As Integer
If Trim$(Character) = "" Then
IsAlpha = False
Exit Function
End If
If Len(Character) = 0 Then
IsAlpha = False
Exit Function
End If
Character = Left$(Character, 1)
If UCase$(Character) >= "A" And UCase$(Character) <= "Z" Then
IsAlpha = True
Exit Function
End If
End Function
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Eugene, your scaring me...
Look at my code.... one line.... let me say that again.... ONE LINE !!!!
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Why dont you use MaskedEdit control? It could work as you intend without further coding....
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Thanks for the inputs guys and for the warm welcome! ;)
I'll try use it in my program.
Thanks a lot! =)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I don't know if I understood your code well Eugene, but what I wanted was the first character would be alphabet and the rest numbers. Not vice versa. When I tried using your code, there was a compile error on function ValidateNumber saying "Wrong number of arguments of invalid property assignment."
Please help.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
As I have said use a maskededit control and use this as the mask:
-
1 Attachment(s)
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Press Ctrl+T, find the Microsoft Masked Edit Control and check it then in your toolbox you could that control as attached, (encircled).....
In the Mask Property of that control specify the mask I've given in my previous post....
-
1 Attachment(s)
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Here is an example. Set the mask the way you want it.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
WoW!!! hehe It worked perfectly fine!!
simple yet awesome.
Though I'd like to know more why there was an error when I used Eugene's suggestion.
Anyway, thanks a lot for the help dee!! ;) ;) ;)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by seraphicmortal
Hi! I'm new in VB6. I would like to know how to make a function that would restrict users to enter a 4 character code and the first character is any of the ALPHABET (A~Z) then the last 3 characters would be NUMERIC (0~9). It must not accept no other symbols and they cannot leave it blank (meaning null). :blush:
Example:
Valid code is C105 --> since first character is an alphabet and the rest is numeric
Invalid codes are 9C32
C+54
null
Here is another way:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error GoTo Text1_KeyPress_Error
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
If Len(Me.Text2.Text) < 1 Then 'Ensures that Firt Character is A through Z
If (KeyAscii < 97 And KeyAscii > 122) And _
(KeyAscii < 65 And KeyAscii > 90) Then
KeyAscii = 0
Else 'All other charaters are only numbers
If (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = KeyAscii - 32 ' Capitalize the first charater
End If
End If
Else
If (KeyAscii > 48 And KeyAscii < 57) Then 'Only allows numbers after
'First Letter
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If
End If
End If
On Error GoTo 0
Exit Sub
Text1_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
End Sub
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
@Mark: There is nothing in your code that restricts me to type a letter in an empty text box and then put the text caret before that letter and start typing digits.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by Joacim Andersson
@Mark: There is nothing in your code that restricts me to type a letter in an empty text box and then put the text caret before that letter and start typing digits.
Joacim,
Thanks, nice catch and here is my solution.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error GoTo Text1_KeyPress_Error
Dim intKey As Integer
intKey = KeyAscii
If intKey <> 8 Then
KeyAscii = 0 ' Cancel Key press here
If Len(Me.Text2.Text) < 1 Then
If (intKey < 97 And intKey > 122) And _
(intKey < 65 And intKey > 90) Then
intKey = 0
Else
If (intKey >= 97 And intKey <= 122) Then
intKey = intKey - 32
End If
End If
Else
If (intKey >= 48 And intKey <= 57) Then
intKey = intKey
Else
intKey = 0
End If
End If
End If
If intKey <> 0 And intKey <> 8 Then
Me.Text2.Text = Me.Text2.Text & Chr(intKey) ' I append the keys to the
'the value in the textbox
End If
On Error GoTo 0
Exit Sub
Text1_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
End Sub
It is a little sloppy but it does the job!! :thumb:
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by Joacim Andersson
@Mark: There is nothing in your code that restricts me to type a letter in an empty text box and then put the text caret before that letter and start typing digits.
Quote:
Originally Posted by Mark Gambo
It is a little sloppy but it does the job!! :thumb:
I cleaned up my code: :D
VB Code:
Option Explicit
'This code sets up the Text Boxes
Private Sub Form_Load()
On Error GoTo Form_Load_Error
Me.Text1.Text = ""
Me.Text2.Text = ""
On Error GoTo 0
Exit Sub
Form_Load_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of Form " & Me.Name
End Sub
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error GoTo Text1_KeyPress_Error
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
Me.Text1.SelStart = Len(Me.Text1.Text) 'This makes sure that characters
'are entered in order (A00000)
If Len(Me.Text1.Text) < 1 Then
If (KeyAscii < 97 And KeyAscii > 122) And _
(KeyAscii < 65 And KeyAscii > 90) Then 'Ensures that First
KeyAscii = 0 'Character is A through Z
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = KeyAscii - 32 ' Capitalize the first charater
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
KeyAscii = KeyAscii 'First Letter
Else
KeyAscii = 0
End If
End If
End If
On Error GoTo 0
Exit Sub
Text1_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
End Sub
I know this is an old post, but I knew I could tighten up the code from my original post and fix the problem that Joacim found. Thanks for indulging me.
(Mark is now returning to obscurity....) :wave:
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Eyh Mark, I think you're getting into something ;)....Your code works great...but can you please restrict the user to only input the 1st character as alpha and the rest numeric??
I used your code in my program and it works the way I wanted but the problem is that if a user entered a numeric as the 1st character input, your code still allows it.
Hope you can add something to your code that will prohibit users from entering numeric as 1st input. I think that is what it lacks..
Hope you can help though.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
He made a mistake in this line:
VB Code:
If (KeyAscii < 97 And KeyAscii > 122) And _
(KeyAscii < 65 And KeyAscii > 90) Then 'Ensures that First
use this:
VB Code:
If (KeyAscii < 97 Or KeyAscii > 122) And _
(KeyAscii < 65 Or KeyAscii > 90) Then 'Ensures that First
Notice the Or's instead of the And's
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I think I have the solution to my question with the help of Mark of course. ;)
Here's the code of Mark with my 'lil revisions: :)
VB Code:
Private Sub txt_symcod_KeyPress(KeyAscii As Integer)
On Error GoTo txt_symcod_KeyPress_Error
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
Me.txt_symcod.SelStart = Len(Me.txt_symcod.Text) 'This makes sure that characters
'are entered in order (A00000)
If Len(Me.txt_symcod.Text) < 1 Then
If (KeyAscii < 97 And KeyAscii > 122) And _
(KeyAscii < 65 And KeyAscii > 90) Then 'Ensures that First
KeyAscii = 0 'Character is A through Z
Else
If (KeyAscii >= 48 And KeyAscii <= 57) Then
MsgBox "Do not enter numeric as 1st character input."
KeyAscii = 0
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = KeyAscii - 32 ' Capitalize the first character
End If
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
KeyAscii = KeyAscii 'First Letter
Else
KeyAscii = 0
End If
End If
End If
On Error GoTo 0
Exit Sub
txt_symcod_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure txt_symcod_KeyPress of " & Me.Name
End Sub
:) Thanks a lot Mark!!! :)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Isn't my solution better ?!?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hi! Baja, I replied quickly and wasn't able to try your suggestion. I tried yours and it does the job as well...
Now, both your codes have a problem..
If the CAPS LOCK is OFF, both your codes works just fine....but when the CAPS LOCK is turned ON, the ALPHA (1st character) being inputted is not displayed....it displays a different character.
example:
(caps lock is on)
user input: A --> txtbox display: !
(caps lock is off)
user input: a --> txtbox display: a
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I cant replicate that. Here is my code. Try it and see if it works:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error GoTo Text1_KeyPress_Error
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
Me.Text1.SelStart = Len(Me.Text1.Text) 'This makes sure that characters
'are entered in order (A00000)
If Len(Me.Text1.Text) < 1 Then
If (KeyAscii < 97 Or KeyAscii > 122) And _
(KeyAscii < 65 Or KeyAscii > 90) Then 'Ensures that First
KeyAscii = 0 'Character is A through Z
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = KeyAscii - 32 ' Capitalize the first charater
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
KeyAscii = KeyAscii 'First Letter
Else
KeyAscii = 0
End If
End If
End If
On Error GoTo 0
Exit Sub
Text1_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
End Sub
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Your the best Baja!! :) Your code worked like a charm! ;)
Thanks a lot! :wave:
Yoroshiku,
seraphicmortal :thumb:
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Guess I'm just to fresh from the Unix/Perl world to let this go by without mentioning regular expressions...
Add a reference to the VBScript Regular Expressions and try this code
VB Code:
Dim reg as New RegExp
reg.Pattern = "^[a-zA-Z][0-9]{3}$"
if reg.Test(input string) = True then
'do whatever
End If
Pretty simple and it will only take 1 alpha to begin the string with 3 numeric characters ending the string
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Isn't it more satisfactory when you have to use your head to solve a problem? :)
And if you only need the check for one thing, why include a new reference for just that one thing.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
One question, can the code of Baja be in a form of a function??
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Oh yeah, always fun to figure something out.
If I was only going to use something one time in a project I wouldn't add another dependancy if there was anyway possible to avoid doing so. I just pointed out the RegExp primarily so that seraph would be aware of their existance in the future and, secondarily, I think that most VB programers would be well served by learning their use. RegExp's are really powerfull for anything dealing with text and, in a lot of cases, are faster (I know that today's thinking is that speed isn't critical but to me, maybe due to me coming from a more internet server sided background, CPU cycles are valuable).
Now seraph...Sure his code will work in a function. If you are wanting to call the code from more than one form I would suggest placing it in a public function in a bas module somethig like this...
VB Code:
Public Function verifyFormat(inString as String) as Boolean
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey, vb -
Do you know of any Windows apps that write RegExp's for you, or allow you to test them to make sure that they are right? I tried to use one, but it didn't work. I don't think it was a Windows app, though.
Any links ?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Ok VB, i'll try ur suggestion. Thanx man! ;)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Baja's code won't work if you paste text into the box with the context menu.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
huh?? what are you talkin about? what's context menu if you don't mind me askin?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey VB could you please elaborate how that function looks like? Please..
Current code (c/o Baja) for textbox input for symbolic code is:
VB Code:
Private Sub txt_symcod_KeyPress(KeyAscii As Integer)
On Error GoTo txt_symcod_KeyPress_Error
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
Me.txt_symcod.SelStart = Len(Me.txt_symcod.Text) 'This makes sure that characters
'are entered in order (A00000)
If Len(Me.txt_symcod.Text) < 1 Then
If (KeyAscii < 97 Or KeyAscii > 122) And _
(KeyAscii < 65 Or KeyAscii > 90) Then 'Ensures that First
KeyAscii = 0 'Character is A through Z
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = KeyAscii - 32 ' Capitalize the first charater
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
KeyAscii = KeyAscii 'First Letter
Else
KeyAscii = 0
End If
End If
End If
On Error GoTo 0
Exit Sub
txt_symcod_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure txt_symcod_KeyPress of " & Me.Name
End Sub
But I have 3 more other textboxes that needs this same validation. I think it would be a bit long if I keep repeating the same codes over and over again...that was why I want to know how to make this into a function.
Hope you can help me.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by seraphicmortal
huh?? what are you talkin about? what's context menu if you don't mind me askin?
Right click on the textbox, and select paste.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
seraphicmortal,
Have you seen This Post yet? It is a function I wrote that will allow you to:
1) Limit the number of characters entered into a textbox
2) Limit the type of Alpha-numeric Characters that can be
entered in a particular position
For example if in one text box you wnat the user to enter data in the following format C89-5923A, using the function I created you would put the following in the Tag Property of the text box: 'A00-0000A'. Try the function out an tell me what you think.
Also the user will not be able to paste data into the textbox.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Here it is in a form of a function and an example of use:
VB Code:
Private Function ValidateKey(KeyAscii As Integer, txtField As TextBox) As Integer
txtField.SelStart = Len(txtField.Text)
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
If Len(txtField.Text) < 1 Then
If (KeyAscii < 97 Or KeyAscii > 122) And _
(KeyAscii < 65 Or KeyAscii > 90) Then 'Ensures that First
ValidateKey = 0 'Character is A through Z
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
ValidateKey = KeyAscii - 32 ' Capitalize the first charater
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
ValidateKey = KeyAscii 'First Letter
Else
ValidateKey = 0
End If
End If
Else
ValidateKey = KeyAscii
End If
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKey(KeyAscii, Text1)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKey(KeyAscii, Text2)
End Sub
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by dglienna
Hey, vb -
Do you know of any Windows apps that write RegExp's for you, or allow you to test them to make sure that they are right? I tried to use one, but it didn't work. I don't think it was a Windows app, though.
Any links ?
Well, dg, I started to say no and then suggest that you check out EditPadPro for it's regexp support but, in verifying I had the correct URL, I noticed that the same folks now have an app called RegexBuddy for $29.95 and that it has a 3 month unconditional money-back gurantee. Based on my experience with EditPadPro I'd say it's worth a shot.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I was hoping for a few links, or a free app. I have no need to use regexp at the moment, but thought i'd look into how it works. Thanks for the suggestion, but I'll have to keep it on file.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I just found this site: http://regexlib.com/ - they supposedly have indexed 960 RegExp's that are suitable for VB and they even have an online tester....
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Many text editors, including my own Source Edit, is able to use regular expressions. You can use the Find in Files feature of Source Edit to test your regular expressions.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
If your looking for a good Regular Expressions site then have a look here - Lots of things there, Sample RegExp's, Test Area and submit your own.
Thats where I get and test all :)
Cheers,
RyanJ
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
JA - Nice features. I noticed the line numbers, and wondered if you have a cross-reference feature built in, or available as an add-in?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by dglienna
JA - Nice features. I noticed the line numbers, and wondered if you have a cross-reference feature built in, or available as an add-in?
Sorry but I don't understand. Cross-reference to what?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Here is an old example from my QB days. It listed every symbol along with every line that it appeared on, after numbering the lines for the .LST version that it produced. You'd just specify XREF BLB30-13 and a minute later, you had the new cross-refernce files (.LST and .REF)
Quote:
Page 71
12-06-1993
BLB30-13.BAS QBX Symbol Listing 22:53:48
Symbol --- Reference(s) ----------------------------------------
COMMENT2$ 1077,1745,2804,2866,3043
CONAME$ 117,1037,1117,1119,1123,1619,2650,2723,2853,3030
CONST 98,99,100
CONVERT.SIZE 22,676,2366
COPYTYPE 183,277
COUNT *2190
Elapsed time: 1:08
Labels: 21
Symbols: 419 ( 2500 maximum)
References: 3804 ( 16380 maximum)
Max fields: 35
Lines: 3292
Memory: 38304 available
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Source Edit currently doesn't have such a feature, however it can list all functions and procedures in an open document (it uses regex to do so btw).
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey! Baja, thanx for the valued inputs..catering my questions and all.. I know the code looks the same but is in function form, but when I used it in my program, it doesn't allow any character entry when CAPSLOCK is turned ON?? but when I used your previous code using keypress, it worked just fine.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Oops. My bad. Here is the new code:
VB Code:
Private Function ValidateKey(KeyAscii As Integer, txtField As TextBox) As Integer
txtField.SelStart = Len(txtField.Text)
If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
If Len(txtField.Text) < 1 Then
If (KeyAscii < 97 Or KeyAscii > 122) And (KeyAscii < 65 Or KeyAscii > 90) Then
ValidateKey = 0
Else
If (KeyAscii >= 97 And KeyAscii <= 122) Then
ValidateKey = KeyAscii - 32 ' Capitalize the first charater
Else
ValidateKey = KeyAscii ' Character already capitalized
End If
End If
Else ' If Keypress is not the First Character and only allows numbers
If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
ValidateKey = KeyAscii 'First Letter
Else
ValidateKey = 0
End If
End If
Else
ValidateKey = KeyAscii
End If
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKey(KeyAscii, Text1)
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKey(KeyAscii, Text2)
End Sub
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Great! Great! Great! Thanks a lot! ;) Can I just disable the right click (hehe...I know it's kinda lousy thing to do)but that's the only way to prevent users from pasting tru context menu.. what do u think?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I think its possible. Do a search on the forums, I think I have seen that asked before.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Even though it is possible to surpress the context menu I don't think that is the way to go. The user could simply paste by pressing Ctrl+V instead. I think you should add additional code in the Change event and inform the user if they've pasted illegal text.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
The Validate method seems appropriate to avoid the ol Cut 'n' Paste issues:
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
If UCase(Left$(Text1.Text, 1)) Like "[A-Z]" Then
Cancel = False
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Cancel = True
End If
End Sub
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Yeah, but Ctrl+V can be surpressed too. There was a big thread about preventing pasting. I'll look for it.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by baja_yu
Yeah, but Ctrl+V can be surpressed too. There was a big thread about preventing pasting. I'll look for it.
X-Post :)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I found it! Hooray for me! Hooray for Zoidberg! :)
Here is exactly what you need: http://www.vbforums.com/showthread.p...=prevent+paste
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
And..... as a Function (used inconjunction with baja_yu's code):
VB Code:
Option Explicit
Private Sub Text1_Validate(Cancel As Boolean)
Cancel = Cancel_Entry(Text1, Text1.Text)
End Sub
Private Sub Text2_Validate(Cancel As Boolean)
Cancel = Cancel_Entry(Text2, Text2.Text)
End Sub
Private Function Cancel_Entry(ByRef txtBox As TextBox, ByVal strBuff As String) As Boolean
If UCase(Left$(strBuff, 1)) Like "[A-Z]" Then
Cancel_Entry = False
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.SelStart = 0
txtBox.SelLength = Len(strBuff)
Cancel_Entry = True
End If
End Function
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
I know you can surpress the pasting all together but I still don't think that is the way to go. I for one would find it very annoying if I couldn't paste text to an input field. Using the keypress event is a good way of stoping the user from inputting invalid data but it's not enough. Using the Validate event as suggested by Bruce Fox is one way but that doesn't surpress a cut or paste command since it doesn't run until the control is about to lose focus to another control that has the CausesValidation property set to True. But you can either use that or a more direct way could be to use the Change event.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by Joacim Andersson
a more direct way could be to use the Change event.
Concur :)
That would give imediate result over the Validate event.
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Ok, how is this:
VB Code:
Option Explicit
Private Sub Text1_Change()
Call Validate_Entry(Text1, Text1.Text)
End Sub
Private Sub Text2_Change()
Call Validate_Entry(Text2, Text2.Text)
End Sub
Private Function Validate_Entry(ByRef txtBox As TextBox, ByVal strBuff As String) As Boolean
If UCase(Left$(strBuff, 1)) Like "[A-Z]" Then
'do nothing
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.SelStart = 0
txtBox.SelLength = Len(strBuff)
End If
End Function
The above takes care of regular keyboard input AND paste operations.
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
The code of Baja I know doesn't prevent users from pasting tru context menu(right click menu) but it certainly doesn't allow people pasting thru CTRL+V. (FYI only)
So, the current dilemma is how to prevent users from right-clicking...
I'll try Bruce Fox's suggestion though...