Need to edge out the competition for your dream job? Train for certifications today.
// Function to validate an e-mail address with the extra rules
// that user name part
// must begin with a letter,
// must end with a letter or numeral,
// may only consist of letters, numerals, dashes and dots and
// may not have consequtive dashes or dots.
// Note -. and .- are illegal consequtive dash/dots.
// Original code found on etutorials.org
// Modified to meet the needs of another member at Experts Exchange.
// Last modification: Hugh McCurdy 21-Aug-2012
#define TRUE (1)
#define FALSE (0)
// return TRUE if and only if c is a period or a dash.
int isdotdash ( const char c )
switch ( c )
} // isdotdash
int spc_email_isvalid(const char *address)
int count = 0;
const char *c, *domain;
static char *rfc822_specials = "()<>@,;:\\\"";
c = address;
// First character must be a letter.
if ( ! isalpha ( *c ))
for ( c = address + 1; *c && *c != '@'; c++ )
// dot/dash check. Consequtive illegal. (--, .., .- and -. illegal)
if ( isdotdash ( *c ))
if ( isdotdash ( *( c - 1 )))
// if not a dot/dash then only alphanumeric characters are legal.
else if ( ! isalnum ( *c ))
} // for
// Make sure the current character is an @
if ( *c != '@' )
// Now make sure the character before the @ is a alphanumeric.
if ( ! isalnum ( *(c - 1 )))
/* next we validate the domain portion (name@domain) */
if (!*(domain = ++c)) return 0;
if (*c == '.')
if (c == domain || *(c - 1) == '.') return 0;
if (*c <= ' ' || *c >= 127) return 0;
if (strchr(rfc822_specials, *c)) return 0;
} while (*++c);
return (count >= 1);
} // spc_email_isvalid()
// main() only used for testing.
int main ( void )
static const char *list =
for ( i = 0; *list [ i ]; i++ )
printf ( "%s %s\n", spc_email_isvalid ( list[i] ) ? "PASS" : "FAIL", list[i] );
} // main()
Open in new window
Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.