locked
Check input format RRS feed

  • Question

  • 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!

    Friday, May 1, 2020 4:39 PM

Answers

  • No, the char array is the wrong answer.  Just do it the way you would do it as a human being.  Strip off the first three characters, remove all the periods, convert the first 8 characters and the last 2 characters to integers, and do the computation.  By the way, your description can be simplified a bit, because what you're describing is the modulo operation.  If (first 8 digits + check digits) % 97 == 0, then it is valid.

    using System;
    using System.IO;
    
    class Program
    {
        static bool checkdigit( string co_num )
        {
            // 0123456789.1234
            // BE XXXX.XXX.XXX
            string digits = co_num.Substring( 3 ).Replace( ".", "" );
            int prefix = Convert.ToInt32( digits.Substring( 0, 8 ) );
            int check = Convert.ToInt32( digits.Substring( 8 ) );
            Console.WriteLine( "{0} {1} {2}", prefix, check, prefix % 97 );
            int computed = (prefix + check) % 97;
            return computed == 0;
        }
    
            
    
        static public void Main(string[] args)
        {
            Console.WriteLine( checkdigit( "BE 0123.456.749" ) );
            Console.WriteLine( checkdigit( "BE 0123.456.750" ) );
        }
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 1, 2020 6:17 PM

All replies

  • No, the char array is the wrong answer.  Just do it the way you would do it as a human being.  Strip off the first three characters, remove all the periods, convert the first 8 characters and the last 2 characters to integers, and do the computation.  By the way, your description can be simplified a bit, because what you're describing is the modulo operation.  If (first 8 digits + check digits) % 97 == 0, then it is valid.

    using System;
    using System.IO;
    
    class Program
    {
        static bool checkdigit( string co_num )
        {
            // 0123456789.1234
            // BE XXXX.XXX.XXX
            string digits = co_num.Substring( 3 ).Replace( ".", "" );
            int prefix = Convert.ToInt32( digits.Substring( 0, 8 ) );
            int check = Convert.ToInt32( digits.Substring( 8 ) );
            Console.WriteLine( "{0} {1} {2}", prefix, check, prefix % 97 );
            int computed = (prefix + check) % 97;
            return computed == 0;
        }
    
            
    
        static public void Main(string[] args)
        {
            Console.WriteLine( checkdigit( "BE 0123.456.749" ) );
            Console.WriteLine( checkdigit( "BE 0123.456.750" ) );
        }
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 1, 2020 6:17 PM
  • Thank you very much!!

    This helps a lot!

    Friday, May 1, 2020 8:40 PM