Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to us Alphanumeric Values in a textbox. The box must contain 4 characters. No more or less. While my code below will work for Alphanumeric. I can't get it to recognize 4 characters.
Can you help?

What I have tried:

public void AlphaNumericString()
       {

           //string value;
           string s = BacBox.Text.ToString();
           int.TryParse(s, out int result);


           Regex r = new Regex("^[a-zA-Z0-9]*$");
           if (r.IsMatch(s) && (s.Length > 3  && s.Length< 5 ))
           {

                   BacBox.Text = s;

           }
          else
           {
               MessageBox.Show("Enter 4 alphanumeric characters only");
           }


       }
Posted
Updated 28-Nov-17 10:48am
Comments
Richard Deeming 30-Nov-17 13:56pm    

Try:
^[a-zA-Z0-9]{4}$

Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
 
Share this answer
 
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900