Thread: How to validate a user's input for name? (Making sure it contains only alphabets)

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    How to validate a user's input for name? (Making sure it contains only alphabets)

    Like the title stated, how can I validate a user's input for name?

    I saw someone posted this in another similar thread.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { if ( validate(buff) ) { // do your thing } else { // complain }}
    I don't understand this part
    Code:
    while ( fgets( buff, BUFSIZ, stdin ) != NULL )
    And, for validate(buff), if I use isalpha() , I guess that won't work right? Since it is only for validating a character.Any clues?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by xeon321 View Post
    Like the title stated, how can I validate a user's input for name?
    Define what a 'name' is.

    I don't understand this part
    Code:
    while ( fgets( buff, BUFSIZ, stdin ) != NULL )
    fgets returns NULL when it fails to get the required input.

    And, for validate(buff), if I use isalpha() , I guess that won't work right? Since it is only for validating a character.Any clues?
    If the only requirment for being validated is to have only characters, iterate over the buf and call isalpha for each letter, immediately returning 0 if isalpha fails.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Define what a 'name' is.
    A human's name, which contains only alphabets. There are no such names as , W1LSON , KYL3 right?
    Let's say, Xeon or manasij .

    fgets returns NULL when it fails to get the required input.

    Ah, I see. Thanks.

    If the only requirment for being validated is to have only characters, iterate over the buf and call isalpha for each letter, immediately returning 0 if isalpha fails.

    You mean by entering
    Code:
    isalpha(buf)
    ?

    It says, char type not compatible with parameter type int.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    No, not isalpha(buf).
    Code:
    for(int i=0; i< strlen(buf); ++i)
        if(!isalpha(buf))
            return 0;
        return 1;
    N.B: I've not tested the above code, so there could be bugs.
    Well, someone would correct me soon, if there are, anyway.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    manasij7479's code should be:
    Code:
    for(int i=0; i< strlen(buf); ++i)
        if(!isalpha(buf[i]))     // missing index
            return 0;
        return 1;
    N.B: I've not tested the above code, so there could be bugs.
    Well, someone would correct me soon, if there are, anyway.
    You're welcome :-).

    But what about names like Peter O'Toole or Daniel Day-Lewis?

    Code:
    $ cat test.c
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int validate(char *buf)
    {
        int i;
        for(i=0; i< strlen(buf); ++i)
            if(!isalpha(buf[i]))     // missing index
                return 0;
            return 1;
    }
    
    int main(void)
    {
        char name1[] = "O'Toole";
        char name2[] = "Day-Lewis";
    
        printf("%d\n", validate(name1));
        printf("%d\n", validate(name2));
    
        return 0;
    }
    $ ./test
    0
    0
    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Code:
    char name1[] = "O'Toole"; char name2[] = "Day-Lewis"; printf("%d\n", validate(name1)); printf("%d\n", validate(name2));
    My question is "How can I validate a user's input for name?"

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by xeon321 View Post
    Code:
    char name1[] = "O'Toole"; char name2[] = "Day-Lewis"; printf("%d\n", validate(name1)); printf("%d\n", validate(name2));
    My question is "How can I validate a user's input for name?"
    Presumably you know how to get user input? The question then becomes, how to validate a string. If you need more characters beyond what isalpha() accepts you can define a string constant with all characters you are willing to accept, then use strchr() to validate each character against the pre-defined string constant.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by AndiPersti View Post
    manasij7479's code should be:
    Code:
    for(int i=0; i< strlen(buf); ++i)
        if(!isalpha(buf[i]))     // missing index
            return 0;
        return 1;

    You're welcome :-).
    Can't believe I missed that.
    I probably thought I messed up something with buf not having '\0' at the end /..etc.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    you can define a string constant with all characters you are willing to accept
    Does it means
    Code:
    char name[]="abcdefghijklmnopqrstuvwxyz"
    Can you show me a code example for this?

    then use strchr() to validate each character against the pre-defined string constant.
    By using strchr(), which means I have to type in every single non alphabet characters myself?

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by xeon321 View Post
    Does it means
    Code:
    char name[]="abcdefghijklmnopqrstuvwxyz"
    Something like that yes, but in that case you can use isalpha(). You only need to do this if you also want to accept characters like - and ' for example, like AndiPersti gave en example of.

    Quote Originally Posted by xeon321 View Post
    Can you show me a code example for this?
    Something like this:

    Code:
    int is_valid_userinput(char *s) {
        const char *valid_chars = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ-'";
        for(; *s; s++) {
            if( strchr(valid_chars, *s) == NULL ) {
                return 0; // the current character is not in valid_chars
            }
        }
        return 1;
    }
    You can also use an array index in the for loop instead of pointers of course, but I think you get the idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate input value
    By aladin in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 09:41 AM
  2. Replies: 5
    Last Post: 12-13-2007, 12:14 PM
  3. How to Validate an Input
    By slowcoder in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 07:33 PM
  4. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 08:30 PM
  5. validate alphabets and numbers
    By sturm100 in forum C Programming
    Replies: 4
    Last Post: 07-09-2002, 08:04 AM