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).
Example:
Valid code is C105 --> since first character is an alphabet and the rest is numeric
Invalid codes are 9C32
C+54 null
Last edited by seraphicmortal; May 24th, 2005 at 08:41 PM.
Reason: RESOLVED!
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.
' 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.
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."
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....
Regards,
™
As a gesture of gratitude please consider rating helpful posts. c",)
Re: How to restrict users to enter first character as alphabet and rest as numeric.
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).
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
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
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.
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!!
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Re: How to restrict users to enter first character as alphabet and rest as numeric.
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.
Originally Posted by Mark Gambo
It is a little sloppy but it does the job!!
I cleaned up my code:
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....)
Last edited by Mark Gambo; May 20th, 2005 at 04:30 PM.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
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..
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
Last edited by seraphicmortal; May 22nd, 2005 at 10:38 PM.
Reason: txt error
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.
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.
Last edited by seraphicmortal; May 24th, 2005 at 12:15 AM.
Reason: deleted extra spaces
Yoroshiku, seraphicmortal
______________________________________________________ Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking if you deem this post helpful. Rate this Post!
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.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Re: How to restrict users to enter first character as alphabet and rest as numeric.
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....