Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to build a form for a user to input an ID number, in the format of 'LLLNNNN' (L=letter, N=number) so for example JNH8383 and anything other than that should be determined as invalid.

Also it needs to check for any ID's beginning with "SOS" and "PAN" when these are entered it needs to set off an error message.

Thanks in advance for any tips.
Posted

You have various options available to you for this sort of feature.

One option could be to use "Masked Text Box"
MSDN - Masked Text Box[^]
However you would still need to write some validation to check for your SOS and PAN values which could be done with a simple "In String" check in the textbox leave event.

Another option could be in the "Validating" event on the text box compare a regular expression to the input and trigger the appropriate response.

User Input Validation in Windows Forms (MSDN)[^]
Regular Expressions Quick Ref - MSDN[^]
 
Share this answer
 
Comments
Member 10686581 21-Mar-14 11:37am    
I don't think masking will work because when the user inputs the ID, I need to display whether it is valid or invalid in another text box.. if I used masking it would let the user only enter in a valid format so it leaves no room to enter an invalid one.

Just looked at the regular expressions, it's so confusing!
You would probably need a regular expression for the ID number validation. You can create one with this[^] if you are not familiar with RegEx already.

For the second part, a simple if statement and the use of the substring()[^] method would suffice for this.
 
Share this answer
 
Use MaskedTextBox:
http://msdn.microsoft.com/en-us/library/vstudio/bb763018%28v=vs.100%29.aspx[^]

To validate control use Control.Validating event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating%28v=vs.110%29.aspx[^]

To display validation error use ErrorProvider component:
http://msdn.microsoft.com/library/system.windows.forms.errorprovider%28v=vs.110%29.aspx[^]

To check if string starts with some letters try this String.StartsWith method.

I hope you find this answer useful :)
 
Share this answer
 
v2
Comments
Member 10686581 21-Mar-14 11:28am    
I don't think masking will work because when the user inputs the ID, I need to display whether it is valid or invalid in another text box.. if I used masking it would let the user only enter in a valid format so it leaves no room to enter an invalid one.
Marcin Kozub 21-Mar-14 11:51am    
So, my question is: Why are you want to let people enter invalid value? Just for checking it's not valid? ;)

If you want to user enter text in some specific way, masking is a very good idea.
You still can display what user enters to another control (ie. Label). You can use TextChanged event of your MaskedTextBox to copy its value to another control while user is entering text to MaskedTextBox.

Maybe you should improve your question if you need to do something else.
Member 10686581 21-Mar-14 12:04pm    
It's not me whos idea it is to let the user input an incorrect format lol, it just needs to be able to display a message saying invalid format.. I tried to create a regular expression for the format of LLLNNNN using a program but it just gives me: [a-zA-Z]{3}[0-9]{4}

Once I get the expression could I just use it like this:

private void textBox5_Validating(object sender, CancelEventArgs e)
{
String AllowedChars = [a-zA-Z]{3}[0-9]{4};
if (Regex.IsMatch(textBox5.Text, AllowedChars))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}
}
Marcin Kozub 21-Mar-14 12:20pm    
Try this code:
var pattern = @"[a-zA-Z]{3}\d{4}";
if (Regex.IsMatch(textBox1.Text, pattern))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}

If you want to learn about Regular Expression you will definitelly find this program useful (also mentioned by Richard C Bishop in his solution):
http://www.ultrapico.com/expresso.htm
Assuming the 3 letters are capital, adapt the following example:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string input = "PSN1234";

        Regex regex = new Regex("^(?!PAN)(?!SOS)[A-Z]{3}\\d{4}$");

   if (regex.IsMatch(input))
      {
          Console.WriteLine("Valid");
      }
        else
        {
            Console.WriteLine("Not valid!");
        }
    }
}

If you want the letters to be case-insensitive, then change the [A-Z] to [A-Za-z]

+++++++++++++++++++++++++++++++++++++++++++++

This is in response to your added request:
The main part of your question - how to validate, is provided above, you have to do the rest,it is very straight forward and basic, adding a button to the form, creating a button click event, putting the validation routine in the button click event handler, and showing of a messgebox etc. Refer the following links:
1. C# Button[^]
2. MessgeBox[^]
 
Share this answer
 
v5
Comments
Member 10686581 21-Mar-14 12:13pm    
Thank you this looks like what I'm looking for but I need the code for windows form.

I have a textbox and a button, once the button is clicked it needs to check what the user has inputted in the textbox, how would I go about adapting your code in this way? I would need to display the valid and invalid outputs in either another textbox or as an error message.
Peter Leow 21-Mar-14 12:26pm    
See my added response in my solution. Do remember to accept this as solution and vote.
Member 10686581 21-Mar-14 12:27pm    
Yeah I've done that now here is the code I have used:

private void btnCheck_Click(object sender, EventArgs e)
{

String AllowedChars = ("^(?!PAN)(?!SOS)[A-Z]{3}\\d{4}$");
if (Regex.IsMatch(txtIdentification.Text, AllowedChars))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}

}

When I enter a PAN or SOS I need it to give me a different output, how do I do this?
Peter Leow 21-Mar-14 12:30pm    
The hint is to use substring method, you can find a lot of example on Google.

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