Textbox Input Validation

Dheepak

Board Regular
Joined
Aug 25, 2013
Messages
64
Hi,

I have a text box which will get the product code of each product. All the product will have 10 digit and first 3 digit will be letters. I want to know how to validate the input entered in the text box is matching the above parameter.

For Example, the product code will be ABC1234567. If i enter in different format like two letters and 5 numbers or totally 11 digits means it should throw an error.

Hope I'll get reply and you have understood my concern. Thanks in advance.

Regards,
Dheepak
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here is the error check code. First it checks if the length is 10 characters, then checks if the last 7 digits make a number.

Code:
Err = 0
    If Len(Text.Value) <> 10 Then
        r = MsgBox("The product code you have entered is not 10 characters", , "Invalid Product Code")
        Err = 1
    ElseIf IsNumeric(Right(Text.Value, 7)) = False Then
        r = MsgBox("Invalid Product Code", , "Invalid Product Code")
        Err = 1
    End If

This is written as if the textbox were on a userform. If it is then you would put in an
Code:
If err=1 then
              exit sub
          end if

or

Code:
If err=0 then
                 'rest of code here
          end if
 
Last edited:
Upvote 0
Sorry roy didn't mean to jump you, I was in excel working on the code, and didn't see you had replied, when I came with my solution.
 
Upvote 0
Thanks a lot jt!!!!!!! it was the one which i expected. Thank you once again.

Best Regards,
Dheepak
 
Upvote 0
Thanks a lot jt!!!!!!! it was the one which i expected. Thank you once again.
Just so you know, the code jtdewhurst is not very robust given it should make sure the user typed in a valid product code. For example, it will permit the following "shaped" values (and a lot more) to be entered into the TextBox (I highlighted the problem parts in red)...

&*@1234567
ABC1234E56
ABC1234D56
ABC$123456
ABC&H12345
ABC123456$

I believe the following code will be more bullet-proof...

Code:
  If Not TextBox1.Text Like "[A-Z[B][COLOR="#FF0000"]a-z[/COLOR][/B]][A-Z[B][COLOR="#FF0000"]a-z[/COLOR][/B]][A-Z[B][COLOR="#FF0000"]a-z[/COLOR][/B]]#######" Then
    MsgBox "The Product Code you entered is not valid!", vbCritical, "Invalid Product Code"
    Exit Sub
  End If
  '
  ' Rest of code goes here
  '

Note... as written, the code will permit upper and lower case letters to be accepted... if you need the entries to be upper case letters only then remove the red highlighted "a-z" characters.
 
Upvote 0
Thanks for that post Rick.
"Like" is a good one to file away for the future. I'd never heard of it before.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top