Reputation: 25
I would like to put a security in the edit or variable field so that the first character always represents one letter and the rest of the characters would be only 3 digits.
So: I would like to be able to type a single letter and then some three digits, K631 or Z987 or T345 etc.
After confirming with the key, the script would check whether the edit field (the variable) contains the required characters if there is no eg the first letter and then 3 digits then the message will be displayed otherwise the message will display the number is correct.
Can anyone help with this?
Upvotes: 1
Views: 65
Reputation: 10360
You can make use of a simple Regex to do so. The regex can be like ^[a-zA-Z][0-9]{3}$
as shown in the following code:
strVar = InputBox("Enter the Key")
Set objReg = New RegExp
objReg.Pattern="^[a-zA-Z][0-9]{3}$" 'If you want only the capital letters, you can rewrite the pattern as ^[A-Z][0-9]{3}$
If Not objReg.Test(strVar) Then
MsgBox "Input is incorrect!!!"
Else
MsgBox "Input is correct"
End If
Set objReg = Nothing
Explanation:
^ - position just before the start of the string
[a-zA-Z] - character class which matches any lower or upper case alphabet
[0-9] - character class which matches a digit
{3} - will repeat the preceding token 3 times exactly. The preceding token in our case is a number(0-9)
$ - position just after the end of the string
Upvotes: 2