Page 1 of 3 123 LastLast
Results 1 to 40 of 100

Thread: How to restrict users to enter first character as alphabet and rest as numeric.

  1. #1

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Resolved 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!

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Use the Like operator

    Example:
    VB Code:
    1. Dim valid_entry As Boollean
    2. valid_entry = Text1.Text Like "[A-Z]###"
    valid_entry will be True if the text in the textbox is correct

  3. #3
    Lively Member
    Join Date
    May 2005
    Posts
    108

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Welcome to the forums ! (by the way )

  5. #5
    Lively Member
    Join Date
    May 2005
    Posts
    108

    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

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

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

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    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! =)

  9. #9

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    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.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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:

    Code:
    ?###
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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....
    Attached Images Attached Images  
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

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

  13. #13

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

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

  14. #14
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

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

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. On Error GoTo Text1_KeyPress_Error
    3.  
    4. If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.     If Len(Me.Text2.Text) < 1 Then 'Ensures that Firt Character is A through Z
    6.         If (KeyAscii < 97 And KeyAscii > 122) And _
    7.                 (KeyAscii < 65 And KeyAscii > 90) Then
    8.             KeyAscii = 0
    9.         Else 'All other charaters are only numbers
    10.             If (KeyAscii >= 97 And KeyAscii <= 122) Then
    11.                 KeyAscii = KeyAscii - 32 ' Capitalize the first charater
    12.             End If
    13.                
    14.         End If
    15.     Else
    16.         If (KeyAscii > 48 And KeyAscii < 57) Then 'Only allows numbers after
    17.                                                                  'First Letter
    18.             KeyAscii = KeyAscii
    19.         Else
    20.             KeyAscii = 0
    21.         End If
    22.     End If
    23.    
    24. End If
    25. On Error GoTo 0
    26. Exit Sub
    27.  
    28. Text1_KeyPress_Error:
    29.  
    30. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    31. 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."


  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  16. #16
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. On Error GoTo Text1_KeyPress_Error
    3. Dim intKey As Integer
    4.  
    5. intKey = KeyAscii
    6.  
    7. If intKey <> 8 Then
    8.     KeyAscii = 0 ' Cancel Key press here
    9.         If Len(Me.Text2.Text) < 1 Then
    10.             If (intKey < 97 And intKey > 122) And _
    11.                     (intKey < 65 And intKey > 90) Then
    12.                 intKey = 0
    13.             Else
    14.                 If (intKey >= 97 And intKey <= 122) Then
    15.                     intKey = intKey - 32
    16.                 End If
    17.                    
    18.             End If
    19.            
    20.         Else
    21.             If (intKey >= 48 And intKey <= 57) Then
    22.                 intKey = intKey
    23.             Else
    24.                 intKey = 0
    25.             End If
    26.         End If
    27. End If
    28.  
    29. If intKey <> 0 And intKey <> 8 Then
    30.     Me.Text2.Text = Me.Text2.Text & Chr(intKey) ' I append the keys to the
    31.                                                                    'the value in the textbox
    32. End If
    33.  
    34. On Error GoTo 0
    35. Exit Sub
    36.  
    37. Text1_KeyPress_Error:
    38.  
    39. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    40. 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."


  17. #17
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Cool 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!!
    I cleaned up my code:

    VB Code:
    1. Option Explicit
    2.  
    3. 'This code sets up the Text Boxes
    4.  
    5. Private Sub Form_Load()
    6. On Error GoTo Form_Load_Error
    7.  
    8.     Me.Text1.Text = ""
    9.     Me.Text2.Text = ""
    10.  
    11. On Error GoTo 0
    12. Exit Sub
    13.  
    14. Form_Load_Error:
    15.  
    16. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of Form " & Me.Name
    17. End Sub

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. On Error GoTo Text1_KeyPress_Error
    3.    
    4. If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.     Me.Text1.SelStart = Len(Me.Text1.Text) 'This makes sure that characters
    6.                                             'are entered in order (A00000)
    7.     If Len(Me.Text1.Text) < 1 Then
    8.         If (KeyAscii < 97 And KeyAscii > 122) And _
    9.                 (KeyAscii < 65 And KeyAscii > 90) Then  'Ensures that First
    10.             KeyAscii = 0                                'Character is A through Z
    11.         Else
    12.             If (KeyAscii >= 97 And KeyAscii <= 122) Then
    13.                 KeyAscii = KeyAscii - 32 ' Capitalize the first charater
    14.             End If
    15.                
    16.         End If
    17.     Else ' If Keypress is not the First Character and only allows numbers
    18.         If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
    19.             KeyAscii = KeyAscii                     'First Letter
    20.         Else
    21.             KeyAscii = 0
    22.         End If
    23.     End If
    24.    
    25. End If
    26. On Error GoTo 0
    27. Exit Sub
    28.  
    29. Text1_KeyPress_Error:
    30.  
    31. MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    32. 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."


  18. #18

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    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.

  19. #19
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    He made a mistake in this line:

    VB Code:
    1. If (KeyAscii < 97 And KeyAscii > 122) And _
    2.                 (KeyAscii < 65 And KeyAscii > 90) Then  'Ensures that First

    use this:

    VB Code:
    1. If (KeyAscii < 97 Or KeyAscii > 122) And _
    2.                 (KeyAscii < 65 Or KeyAscii > 90) Then  'Ensures that First

    Notice the Or's instead of the And's

  20. #20

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Resolved 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:
    1. Private Sub txt_symcod_KeyPress(KeyAscii As Integer)
    2.     On Error GoTo txt_symcod_KeyPress_Error
    3.    
    4. If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.     Me.txt_symcod.SelStart = Len(Me.txt_symcod.Text) 'This makes sure that characters
    6.                                             'are entered in order (A00000)
    7.     If Len(Me.txt_symcod.Text) < 1 Then
    8.         If (KeyAscii < 97 And KeyAscii > 122) And _
    9.                 (KeyAscii < 65 And KeyAscii > 90) Then  'Ensures that First
    10.             KeyAscii = 0                                'Character is A through Z
    11.         Else
    12.             If (KeyAscii >= 48 And KeyAscii <= 57) Then
    13.                     MsgBox "Do not enter numeric as 1st character input."
    14.                     KeyAscii = 0
    15.                 Else
    16.                     If (KeyAscii >= 97 And KeyAscii <= 122) Then
    17.                         KeyAscii = KeyAscii - 32 ' Capitalize the first character
    18.                     End If
    19.             End If
    20.                
    21.         End If
    22.     Else ' If Keypress is not the First Character and only allows numbers
    23.         If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
    24.             KeyAscii = KeyAscii                     'First Letter
    25.         Else
    26.             KeyAscii = 0
    27.         End If
    28.     End If
    29.    
    30. End If
    31. On Error GoTo 0
    32. Exit Sub
    33.  
    34. txt_symcod_KeyPress_Error:
    35.  
    36. MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure txt_symcod_KeyPress of " & Me.Name
    37. End Sub

    Thanks a lot Mark!!!

  21. #21
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Isn't my solution better ?!?

  22. #22

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    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

  23. #23
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     On Error GoTo Text1_KeyPress_Error
    3.        
    4.     If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.         Me.Text1.SelStart = Len(Me.Text1.Text) 'This makes sure that characters
    6.                                                 'are entered in order (A00000)
    7.         If Len(Me.Text1.Text) < 1 Then
    8.             If (KeyAscii < 97 Or KeyAscii > 122) And _
    9.                     (KeyAscii < 65 Or KeyAscii > 90) Then  'Ensures that First
    10.                 KeyAscii = 0                                'Character is A through Z
    11.             Else
    12.                 If (KeyAscii >= 97 And KeyAscii <= 122) Then
    13.                     KeyAscii = KeyAscii - 32 ' Capitalize the first charater
    14.                 End If
    15.                    
    16.             End If
    17.         Else ' If Keypress is not the First Character and only allows numbers
    18.             If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
    19.                 KeyAscii = KeyAscii                     'First Letter
    20.             Else
    21.                 KeyAscii = 0
    22.             End If
    23.         End If
    24.        
    25.     End If
    26.     On Error GoTo 0
    27.     Exit Sub
    28.    
    29. Text1_KeyPress_Error:
    30.    
    31.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Text1_KeyPress of " & Me.Name
    32. End Sub

  24. #24

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Resolved 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!

    Yoroshiku,
    seraphicmortal
    Last edited by seraphicmortal; May 22nd, 2005 at 10:29 PM. Reason: [RESOLVED]

  25. #25
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    No problem

  26. #26
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    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:
    1. Dim reg as New RegExp
    2. reg.Pattern = "^[a-zA-Z][0-9]{3}$"
    3. if reg.Test(input string) = True then
    4.      'do whatever
    5. End If

    Pretty simple and it will only take 1 alpha to begin the string with 3 numeric characters ending the string

  27. #27
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  28. #28

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

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

  29. #29
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    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:
    1. Public Function verifyFormat(inString as String) as Boolean

  30. #30
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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 ?

  31. #31

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Ok VB, i'll try ur suggestion. Thanx man!

  32. #32
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

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

  33. #33

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    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?
    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!

  34. #34

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Question 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:
    1. Private Sub txt_symcod_KeyPress(KeyAscii As Integer)
    2.     On Error GoTo txt_symcod_KeyPress_Error
    3.  
    4.     If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.         Me.txt_symcod.SelStart = Len(Me.txt_symcod.Text) 'This makes sure that characters
    6.                                                 'are entered in order (A00000)
    7.         If Len(Me.txt_symcod.Text) < 1 Then
    8.             If (KeyAscii < 97 Or KeyAscii > 122) And _
    9.                     (KeyAscii < 65 Or KeyAscii > 90) Then  'Ensures that First
    10.                 KeyAscii = 0                                'Character is A through Z
    11.             Else
    12.                 If (KeyAscii >= 97 And KeyAscii <= 122) Then
    13.                     KeyAscii = KeyAscii - 32 ' Capitalize the first charater
    14.                 End If
    15.  
    16.             End If
    17.         Else ' If Keypress is not the First Character and only allows numbers
    18.             If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
    19.                 KeyAscii = KeyAscii                     'First Letter
    20.             Else
    21.                 KeyAscii = 0
    22.             End If
    23.         End If
    24.  
    25.     End If
    26.     On Error GoTo 0
    27.     Exit Sub
    28.  
    29. txt_symcod_KeyPress_Error:
    30.     MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure txt_symcod_KeyPress of " & Me.Name
    31. 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!

  35. #35
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

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

  36. #36
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

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


  37. #37
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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:
    1. Private Function ValidateKey(KeyAscii As Integer, txtField As TextBox) As Integer
    2.     txtField.SelStart = Len(txtField.Text)
    3.    
    4.     If KeyAscii <> 8 Then 'Checks if Backspace key was pressed
    5.         If Len(txtField.Text) < 1 Then
    6.             If (KeyAscii < 97 Or KeyAscii > 122) And _
    7.                     (KeyAscii < 65 Or KeyAscii > 90) Then  'Ensures that First
    8.                 ValidateKey = 0                                'Character is A through Z
    9.             Else
    10.                 If (KeyAscii >= 97 And KeyAscii <= 122) Then
    11.                     ValidateKey = KeyAscii - 32 ' Capitalize the first charater
    12.                 End If
    13.  
    14.             End If
    15.         Else ' If Keypress is not the First Character and only allows numbers
    16.             If (KeyAscii >= 48 And KeyAscii <= 57) Then 'Only allows numbers after
    17.                 ValidateKey = KeyAscii                      'First Letter
    18.             Else
    19.                 ValidateKey = 0
    20.             End If
    21.         End If
    22.     Else
    23.         ValidateKey = KeyAscii
    24.     End If
    25. End Function
    26.  
    27. Private Sub Text1_KeyPress(KeyAscii As Integer)
    28.     KeyAscii = ValidateKey(KeyAscii, Text1)
    29. End Sub
    30.  
    31. Private Sub Text2_KeyPress(KeyAscii As Integer)
    32.     KeyAscii = ValidateKey(KeyAscii, Text2)
    33. End Sub

  38. #38
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    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.

  39. #39
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  40. #40
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

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

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width