-
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...
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Obviously things have evolved since this post began.
I have re-read (quickly) and think that the last example I posted will infact
do what you after (in it's entirity).
It will alert the user if they enter anything other than an Alpha character at the start of the entry.
It will also cater for ANY paste operation, alerting the user if unwanted data was pasted.
In short (from the tests I did) you need no other code at all to validate the entry.
Good luck with your project :)
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey Bruce! Indeed this thread has gone a long way!! Thanks to all the good people out here. :wave:
Do you mean that all I need is this code below that you posted??
Quote:
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.
What about of Baja's code?
Well, I tried yours alone and sad to say it's not working as you said it would...
Hope you could re-check...
Thanks!
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Actually my code (not my code, the code from the link I provided) works!
The context menu does show, but past will yield no results. It will not paste the data.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by seraphicmortal
Do you mean that all I need is this code below that you posted??
Yep...
Quote:
Originally Posted by seraphicmortal
Well, I tried yours alone and sad to say it's not working as you said it would...
Hmmm, I successfully tested it (a few times) prior to posting it. What isn't it doing
that it should? (maybe something I missed in the translation) :)
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
And.... a slight tweek:
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]" Or Left$(strBuff, 1) = vbNullString Then
'do nothing
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.Text = ""
End If
End Function
I retested with pasting say "123today" and general keyboard (KeyPresses) of different characters.
All leading Numeric entries were blocked and an alert given.
Bruce.
-
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.
Well, your code sure doesn't allow text entry tru context menu (right-click) that doesn't conform to the (alpha then rest numeric) format and if 1st key-in is an integer BUT when you just key-in "abcd" (all alpha characters, your code still accepts that string.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Ok :)
VB Code:
Option Explicit
Private Sub Text1_Change()
Call Validate_Entry(Text1)
End Sub
Private Sub Text2_Change()
Call Validate_Entry(Text2)
End Sub
Private Sub Validate_Entry(ByRef txtBox As TextBox)
If Left$(txtBox.Text, 1) <> vbNullString Then
If UCase(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#") And Len(txtBox.Text) < 6 Then
'Do nothing
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.Text = ""
End If
End If
End Sub
Input format = annnn
Maximum of 5 characters (first Alpha, remaing Numeral)
Bruce.
-
1 Attachment(s)
Re: How to restrict users to enter first character as alphabet and rest as numeric.
If you're passing a reference to the text box to the function why do you need to pass the text as well? Can't the function just read the Text property? Why have you made this into a function when you never return anything? I also don't like that the function clears the textbox everytime you enter a bad character. It would be better to just indicate that the text format is wrong in some way. Perhaps by changing the color of the textbox to red.
I also think that you should combine Bruce's code with the one you had earlier from baja_yu that restricted input during the KeyPress event. The Change event can then be used as an extra precausion if the user have pasted illegal characters.
I also like to wrap these kind of things up in a handy class that handles everything. That way you can simply create an object of the class initilize it and then forget about it :)
I've attached such a class that uses Bruce's and Baja_yu's code with only slight changes. The changes is mainly cosmetic, first of all instead of only allowing you to type at the end of the text as baya_yu's code you can put the text caret anywhere in the text and just start typing. If you are at the beginning of the text and press a alpha key it will automatically replace the first character with the new one. If you paste text that is invalid the backcolor of the textbox will turn red (by default, you can change that color if you wish). As an extra optional feature you can assign your own popup menu that will replace the default, so you can remove the Cut and Paste if you wish by simply create your own menu. This is how you use it:
VB Code:
Private oValidateText As CTextFormat
Private Sub Form_Load()
Set oValidateText = New CTextFormat
Set oValidateText.TextBox = Text1
End Sub
That's it, the rest is done by the class. The class has 3 properties you can set.
* TextBox = Assign the text box as shown above.
* ContextMenu = Assign a custom popup menu (using the Set keyword)
* ErrorColor = Assign the color the textbox will get if you paste invalid text (default is red)
The class also have one method called Init which you can use to set all the above properties at once, like this:
VB Code:
Call oValidateText.Init(Text1, vbYellow, mnuPopUp)
Only the first argument of the Init method is required the other two are optional. If you assign your own context menu the Paste command will automatically disappear (if you don't have a Paste in your menu of course) and you will not be able to paste using Ctrl+V either.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
@Joacim. Oh, I totaly missed the (misuse) of the Function - my bad, was a hangover from the true functio (that returned a Boolean)
and the Text by Ref certanly can be used, I just (inadvertantly) followed what I had from the start.
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hi! Joacim! I'd like to know if it would possible for you to just post the codes (using vb tags) for easy viewing...and basically because I can't download zip files...hehe sorry for that. Please... ;)
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
OK, create a new class and paste this code to it.
VB Code:
Option Explicit
Private WithEvents txtBox As TextBox
Private mnuContext As Menu
Private m_ErrorColor As Long
Public Sub Init(TextBox As TextBox, _
Optional ErrorColor As Long = vbRed, _
Optional ContextMenu As Menu = Nothing)
Set mnuContext = ContextMenu
m_ErrorColor = ErrorColor
Set txtBox = TextBox
End Sub
Public Property Get ErrorColor() As Long
ErrorColor = m_ErrorColor
End Property
Public Property Let ErrorColor(nNew As Long)
m_ErrorColor = nNew
End Property
Public Property Set TextBox(txt As TextBox)
Set txtBox = txt
End Property
Public Property Get TextBox() As TextBox
Set TextBox = txtBox
End Property
Public Property Set ContextMenu(mnu As Menu)
Set mnuContext = mnu
End Property
Public Property Get ContextMenu() As Menu
Set ContextMenu = mnuContext
End Property
Private Sub Class_Initialize()
m_ErrorColor = vbRed
End Sub
Private Sub txtBox_Change()
Dim nSelStart As Long, nSelLen As Long
If Len(txtBox.Text) Then
If (UCase$(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#")) Then
If UCase$(txtBox.Text) <> txtBox.Text Then
nSelStart = txtBox.SelStart
nSelLen = txtBox.SelLength
txtBox.Text = UCase$(txtBox.Text)
txtBox.SelStart = nSelStart
txtBox.SelLength = nSelLen
End If
txtBox.BackColor = vbWindowBackground
Else
txtBox.BackColor = m_ErrorColor
End If
Else
txtBox.BackColor = vbWindowBackground
End If
End Sub
Private Sub txtBox_KeyPress(KeyAscii As Integer)
Dim nSelLen As Long
nSelLen = txtBox.SelLength
If txtBox.SelStart = 0 And txtBox.SelLength = 0 Then
'This will ensure that the first character is replaced if
'the user has moved the text caret to the beginning of the text
txtBox.SelLength = 1
End If
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
Select Case KeyAscii
Case vbKeyBack
txtBox.SelLength = nSelLen
Case vbKeyA To vbKeyZ
If txtBox.SelStart <> 0 Then
KeyAscii = 0
txtBox.SelLength = nSelLen
End If
Case vbKey0 To vbKey9
If txtBox.SelStart = 0 Then
KeyAscii = 0
txtBox.SelLength = nSelLen
End If
Case Else
KeyAscii = 0
txtBox.SelLength = nSelLen
End Select
End Sub
Private Sub txtBox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not mnuContext Is Nothing Then
If Button And vbRightButton Then
'surpress the default context menu and show our custom menu instead
txtBox.Enabled = False
txtBox.Enabled = True
txtBox.Parent.PopupMenu mnuContext
End If
End If
End Sub
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Hey Bruce, Your code worked great!!! ;) It does take care the pasting issue. One thing though, is it possible to change the alpha(1st Character input) capital as the user would type it or paste it?? Can you amend your code please?
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Joacim,
I have since fixed up the input limit too, maybe you can roll this into the class code.
VB Code:
If UCase(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#") [b]And Len(txtBox.Text) < 6[/b] Then
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
My first thought....
VB Code:
Option Explicit
Private Sub Text1_Change()
Call Validate_Entry(Text1)
End Sub
Private Sub Text2_Change()
Call Validate_Entry(Text2)
End Sub
Private Sub Validate_Entry(ByRef txtBox As TextBox)
If Left$(txtBox.Text, 1) <> vbNullString Then
If UCase(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#") And Len(txtBox.Text) < 6 Then
txtBox.Text = UCase(txtBox.Text)
txtBox.SelStart = Len(txtBox.Text)
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.Text = ""
End If
End If
End Sub
However, Joacim has provided a portable option - the class can be tweeked to suit other apps too :)
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by Bruce Fox
I have since fixed up the input limit too, maybe you can roll this into the class code.
I didn't find that necassary since you could just as well set the MaxLength property of the TextBox and it will fix that automatically.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Quote:
Originally Posted by Joacim Andersson
I didn't find that necassary since you could just as well set the MaxLength property of the TextBox and it will fix that automatically.
Good point ;)
Bruce.
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Thank you so much you guys esp. to you Bruce!! ;) I think you code covers all the issues in my question. Bravo to you!!
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
wellllll, since everyone one else has been messing with this, I thought I'd give it a try :)
VB Code:
Private Sub Text1_Change()
Text1.Text = TestFormat(Text1.Text)
End Sub
Private Function TestFormat(ByVal Text As String) As String
Dim lMaxLen As Long
lMaxLen = 4 ' or how ever long you want it to be
If Len(Text) > lMaxLen Then Exit Function
If InStr(1, "abcdefghijklmnopqrstuvwxyz", Left$(Text, 1), vbTextCompare) Then
If IsNumeric(Mid$(Text, 2)) Or Len(Text) = 1 Then
Mid$(Text, 1, 1) = UCase$(Mid$(Text, 1, 1))
TestFormat = Text
End If
End If
End Function
-
Re: How to restrict users to enter first character as alphabet and rest as numeric.
Yeah, but your code erases the whole input if an invalid character is entered.
Most users would find that extremely annoying.