Thread: Validation of password please help

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    5

    Exclamation Validation of password please help

    program should validate the input data (password) based on the following rules: The password length should have a minimum length of 6 and maximum of 15 characters Some characters aren’t allowed in the password including “space” and “-”. If user includesthese two characters in the input, the program should display “invalid password”.
    Code:
    int main()
    {
      char password[15];
      int lenght;
    
      int str;
      printf("Please enter the password:\n");
      printf("note:no space or - in password\n");
      printf("note:min password lenght =6 and max password lenght =15\n");
      fgets(password, 15, stdin);
      int i = strlen(password);
    
      if (i < 5 || i > 14) {
        printf("please check the lenght rules ");
    
      }
    
      else if (i > 0) {
        for (int j = 0; j <= i; j++) {
          password[j] == ' ';
          password[j] == '-';
    
        }
        printf("No space is allowed or any hypens - ,please try again");
      } else
    
        printf("registered");
    
    }
    Last edited by Salem; 06-30-2019 at 12:25 PM. Reason: Remove crayola

  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
    Some points.
    1. To read 15 characters with fgets(), you need a buffer of 17 chars. You need the +2 for the "\n\0" that's also stored.

    2. Try something like if ( password[j] == ' ' )

    3. Put the relevant part of the code in a while loop, along the lines of
    Code:
    int passwordValid = 0;
    while ( !passwordValid ) {
        // input
        // checking
        //....
        passwordValid = 1;  // do this when ALL your checks pass.
    }
    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
    Registered User
    Join Date
    Jun 2019
    Posts
    5
    Code:
    /*im doing something wrong ? im new to it */
    
    #include <stdio.h>
    #include<string.h>
    
    
    int main()
    {
        char password[17];
       int lenght;
       int passwordValid = 0;
    
    
      
       int str;
        
        while ( !passwordValid ) 
        { 
          
          printf("Please enter the password:\n");
        printf("note:no space or - in password\n");
         printf("note:min password lenght =6 and max password lenght =15\n");
        fgets(password,15,stdin);
         int i= strlen(password);
         if(i<5 || i>14) 
          {
            printf("please check the lenght rules ");
          
          }
          
         else if(password[i]=' ')
          {
            
            printf("No space is allowed or any hypens - ,please try again");
          }
          
          else if(password[i]='-')
          {
            
            printf("No space is allowed or any hypens - ,please try again");
          }
            else
            {
              printf("registered");
            }
              passwordValid = 1;
        }  
         
    }

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    2
    Try to change the size of an array by +2, in this way you can able to validate password.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Take a look at strpbrk().

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. You need a for loop to check each character.

    2. You managed to slip from using == for comparison to using =
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Note that fgets() keeps the linefeed ('\n') character at the end of the string. You can easily remove that with this line of code after fgets():

    Code:
    password[strcspn(password, "\n")] = '\0';
    Also, why do you compare the password's length with 5 and 14? If I entered the password "123456789012345", for example, your code will reject that password, even though it's valid according to the given rules.

    Lastly, I hope you know that this is just a programming exercise and that you don't think that this sort of password validation is good practice. A password should be allowed to be longer than 15 characters, and many good passwords contain spaces or other "special" characters. Some of my passwords are longer than 20 characters and contain spaces.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2014, 02:08 AM
  2. Validation
    By ulti-killer in forum C Programming
    Replies: 8
    Last Post: 11-15-2012, 12:06 AM
  3. validation
    By yjn in forum C Programming
    Replies: 1
    Last Post: 03-16-2012, 08:37 AM
  4. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  5. validation
    By Ideswa in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2006, 01:01 PM

Tags for this Thread