Resolved check input format

BelgianProgrammer

New member
Joined
May 1, 2020
Messages
2
Programming Experience
Beginner
I am trying to make a program with WPF where the user can enter a Belgian company number. The program should be able to check if the input is the correct format. The format should be "BE 0XXX.XXX.XXX" where X represents an integer. The position of the white space and the points are important.

The validity of the company number can also be checked by a calculation:

for example the company number is BE 0123.456.749.

(first 7 integers of the company number) 1234567 / 97 = 12727.49485

12727 * 97 = 1234519

1234567 - 1234519 = 48

97 - 48 = 49 (last 2 integers of the company number)

when we devide the first 7 integers after the 0 (so 1234567) by 97, we get 12727(.49485). We drop the number after the comma. then we multiply the outcome with 97, we get 1234519. Now we take the original 7 integers (1234567) and subtract 1234519 form it, we get 48. Last step: 97 - 48 = 49. These are the last 2 integers from the company number. That's how we know that the number is valid.

I tried to put the user-input in a char array so that i can use it for the calculation. But i'm seriously stuck. My program doesn't work, whatever I try... Does anyone have some advise for me?

Thank you!
 
First of all, share your current code to show us what you have done. Without that, it looks like you are just begging for code and want someone else to do it for you.

Next, you are partially on the right track by getting the input into a character array. Since the input has a fixed format, it makes things much easier to extract the pieces and perform the check.

Since, we encourage posters to come to their own solutions, I'll walk you through a general outline of how to do things. So if you have the character array input, you will have something like this:
C#:
input[0] : 'B'
input[1] : 'E'
input[2] : ' '
input[3] : '0'
input[4] : '1'
input[5] : '2'
input[6] : '3'
input[7] : '.'
input[8] : '4'
input[9] : '5'
input[10] : '6'
input[11] : '.'
input[12] : '7'
input[13] : '4'
input[14] : '9'

Let's start with an easy example of cherry picking characters and building a string: To get the "49", you would create a string by concatenating the last two digits:
C#:
string suffixString = "" + input[13] + input[14];
After that is executed, suffixString should contain "49".
So next you parse that value into an integer:
C#:
int suffixValue = 0;
if (!int.TryParse(suffixString, out suffixValue))
{
    // handle failed parsing here
}
After executes successfully, then suffixValue should have an integer value of 49.

So next you would do the same thing with the company number:
C#:
string prefixString = "" + input[4] + input[5] + input[6] + input[8] + input[9] + ... ;
The "..." above is left as an exercise for you. :)
Then you'll want to follow the same process of converting that string value into an integer value prefixValue.

When you have the prefixValue value, it is a simple matter of checking if the following is true:
C#:
int bucketSize = 97;
int bucketNumber = prefixValue / bucketSize
if ( bucketNumber * bucketSize + suffixValue != prefixValue)
{
    // handle case when company number is not valid
}

The other parts of the cherry picking that you need to do is to make sure that characters at input[0], input[1], input[2], input[3], etc. are the characters that are expected for a valid company number. Alternatively, you can use a regular expression match to perform this kind of check.

And then once you have all of that logic for determining whether a company number entered is valid, you need to hook in some WPF binding validation.
 
Last edited:
Alright thank you!! That helps! I actually had my code and was kind of on the right track. I don't want people to think that I'm begging for code ?, so next time I will show the code that I have made so far! (Thanks for the tip ?)
 
Back
Top Bottom