Thread: need help in validation

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    1

    need help in validation

    Code:
    void check_nric(char *string_check, int length_check)
    {
    
    	  int wrong, length, i;
    	  int ctr = 0;
    	  char field[15] = "NRIC Number";
    	  do{
    	
    if(ctr != 0)
    {
    printf("\n\tPlease Enter [NRIC Number] Again: ");
    }
    new_get_nric(string_check,length_check,field);
    
    wrong = 0;  /* reset flag at top of loop */
    
    length = strlen(string_check);
    
    /*used to check if all digits are entered*/
    for (i = 0; i < length; i++)   /* <-------------- Using the length */
    {
         if ( !isdigit(string_check[i]) && strchr(string_check,'-') == NULL)
              {
    	wrong = 1;
    	printf("\n\tUse only NUMBERS");
    	 break;
             }
    }
    
    
    
    ctr++;
    /* note: the checking for space here is not necessary
    * as the a [space] is not a valid numera thus an error will occur
    */
             }while(wrong);
    
    }
    As you see i am this code will check the nric number that the user enters and it must be only digits.

    How if i want to make the code as the nric number input must be 14 digits only and not less or more, and include the '-' symbol.'

    Ex: 840402-10-5895

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First thing this code needs is a consistent approach to indentation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How if i want to make the code as the nric number input must be 14 digits only and not less or more, and include the '-' symbol.'
    This would be a good start:
    Code:
    #include <string.h>
    
    int valid_nric ( const char *nric )
    {
      size_t len = strlen ( nric );
      size_t match = strspn ( nric, "0123456789-" );
    
      if ( len != 14 || match != 14 )
        return 0;
    
      return 1;
    }
    However, if the nric must have a specific number of digits separated by '-', for example, six digits followed by one dash followed by two digits followed by one dash followed by four digits, the above code is too relaxed. You would need to do a little bit more, though not much. Using the above function, a more restrictive solution is clear:
    Code:
    int valid_nric ( const char *nric )
    {
      size_t len = strlen ( nric );
      size_t match = strspn ( nric, "0123456789-" );
    
      if ( len != 14 || match != 14 )
        return 0;
      /*
       * If it isn't a digit, it must be a dash */
       */
      match = strspn ( nric, "0123456789" );
      if ( match != 6 )
        return 0;
      nric += match + 1;
      match = strspn ( nric, "0123456789" );
      if ( match != 2 )
        return 0;
      nric += match + 1;
      match = strspn ( nric, "0123456789" );
      if ( match != 4 )
        return 0;
    
      return 1;
    }
    And it is a good idea to avoid using tabs in your code. If you must use tabs, convert them to spaces if your editor supports that feature. Tabs are evil.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation of TextBoxes!?
    By c9dw2rm8 in forum C# Programming
    Replies: 1
    Last Post: 04-01-2008, 06:19 PM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM