TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Robert Creswell
from: andrew clarke
date: 1995-12-24 17:06:36
subject: c

21 Dec 95 07:10, Paul Edwards wrote to Robert Creswell:

 RC>> Can anyone give me an example of how you would validate someones 
 RC>> name in a interactive program using isalpha

 RC>>               eg.     printf("Enter 
 RC>> name:__________\b\b\b\b\b\b\b\b\b\b");                      
 RC>> scanf("%s", name);
 >                           if (valid_name(name))
 >                           { 
 >                              ...
 >                           }

/*
*  validname.c
*
*  Validate a someone's name (`English' names only).
*
*  Written in Christmas Eve 1995 by Andrew Clarke.
*  Released to the public domain.
*
*/

#include 
#include 
#include 
#include 

int valid_name(char *name)
{
    if(!name || strlen(name) < 3)
        return 0;

    while (*name != '\0') {
        if (*name != '-' && *name != '\'' &&
!isspace(*name) && !isalpha(*name))
            return 0;
        name++;
    }

    return 1;
}

int main(void)
{
    char name[80];
    printf("User name: ");
    gets(name);
    if (valid_name(name)) {
        printf("`%s' is a valid name.\n", name);
    } else {
        printf("`%s' is NOT a valid name.\n", name);
    }
    return EXIT_SUCCESS;
}

valid_name() could still do with a bit of work, but it's a bit of an
improvement over Paul's version.  You did test it, right Paul? :-)

The above valid_name() won't work properly with user names such as
"Mikael St†ldal"; it doesn't recognise IBM extended ASCII
characters.  That could be fixed with a little effort though, provided
portablity isn't an issue.  Also, names such as "---" or
"'-'" are seen as valid by the function (I added support for the
- and \' characters to validate names such as "Paul
Speed-Edwards-Grimsley-Lawrence" and "Paul D'Edwards").  No
doubt there is a way to fix this too, but I'm going to leave the hard work
up to you. :-)  Have fun + ZeeYa.

--- Msgedsq/2 3.30
* Origin: Blizzard of Ozz, Melbourne, Australia (3:633/267.1{at}fidonet)
SEEN-BY: 50/99 620/243 623/630 632/348 360 998 633/154 260 267 371 373 374
SEEN-BY: 634/384 635/301 502 503 544 639/252 711/401 409 410 413 430 510 808
SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 633/267 374 371 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.