-
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....