Click here to Skip to main content
13,665,820 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi Everyone
How can i Make a textbox that accepts at first 4 letters and after that 7 digits otherwise give the user an error

i have no idea what to do please put me on the right direction

What I have tried:

i tried to use masked textbox but it didn't work

if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
Posted 2-Jan-18 22:57pm
Updated 2-Jan-18 23:38pm
v4
Comments
Samuel Corpuz 3-Jan-18 20:01pm
   
Use ^[a-zA-Z]{4}\d{7}$ this. to Maximize the Shorter code
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your regex only works for digits, and doesn't care how many there are ,provided there is at least one somewhere in the string.
Try this:
^[a-zA-Z]{4}\d{7}$
  Permalink  
Comments
3-Jan-18 9:27am
   
Finally i found the solution

if (txtContainerNo.TextLength < 4)
{
e.Handled = !(char.IsUpper(e.KeyChar) || e.KeyChar == (char)Keys.Back);


}

else
{
e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back);
}
OriginalGriff 3-Jan-18 10:01am
   
So... I type "ABCD", the use the mouse to click in the middle and type "1234567" you will allow "AB1234567CD" through?

Don't do "while you type" validation: it only confuses the user. Use the regex I gave you in the Validating event.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

if (txtContainerNo.TextLength < 4)
         {
             e.Handled = !(char.IsUpper(e.KeyChar) || e.KeyChar == (char)Keys.Back);


         }

         else
         {
             e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back);
         }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) || (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
       {
           charCount++;
       }


       else if (e.KeyCode == Keys.Back)
       {
           charCount--;
       }

       else
       {

       }




       if (charCount <= 4)
       {
           if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z && e.Shift && e.KeyCode == Keys.Back)
           {



           }
           else
           {
               e.SuppressKeyPress = true;
           }

       }


       else if (charCount > 4)
       {
           if (!(e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z && e.KeyCode != Keys.Back))
           {



           }
           else
           {
               e.SuppressKeyPress = true;
           }
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 7 Jan 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100