Acceptable format putting in textbox : 00-00-mach-0-00
Where, from the left:
00
year00
project numbermach
just a tag0
machine number, must be 1, 2, 3, or 400
pressure
Acceptable format putting in textbox : 00-00-mach-0-00
Where, from the left:
00
year00
project numbermach
just a tag0
machine number, must be 1, 2, 3, or 400
pressureYou'll need to split the input. One approach is to use String.Split
with a -
as the split character.
string input[] = inputString.Split('-');
You can then check that you've got 5 sub strings:
if (input.Length != 5)
{
// Incorrect format
}
And then check each sub strings for the correct format. For example to check that the 4th sub strings is a digit and either 1, 2, 3 or 4:
int number;
if (Int32.TryParse(input[3], out number))
{
if (number < 1 || number > 4)
{
// Incorrect format
}
}
Do this when the text box loses focus.
You could create a regular expression that represents the rules you are trying to enforce and check whether the input matches whenever the Validating-event fires.
I would use a MaskedTextBox control. In the properties box for the control, click on Mask and set the Mask Description to Custom. This mask should work for you: "00-00-m\ach-0-00" You would probably need to add additional validity checks afterwords using the split method above. But it should save you some additional coding.
I would do several things in such a case: