Regex - Only allow a-zAZ0-9 in e-mail address validation

Discussion in 'C' started by BernardoCR, Jul 11, 2011.

  1. BernardoCR

    BernardoCR New Member

    Joined:
    Jul 11, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I am running a program that makes the registration of users with their e-mail address. It makes the registration using mysql.

    The problem is that when someone types the e-mail address with special characters, such as {, ƒ and so on, the program gets killed, returning syntax error in mysql.

    I was wondering if someone could help me fixing the code.

    I tried several ways, but could not make it.

    Here is the code:

    Code:
    int validate_email(const char *email)
    {
    if (strchr(email, '\\'))
    return 0;
    else if (strchr(email, '\"'))
    return 0;
    else if (strchr(email, '\''))
    return 0;
    else if (strchr(email, '@') && strchr(email, '.'))
    return 1;
    else
    return 0;
    } 
    With this code I can filter \, ' and ", but not other special chars.

    Thank you!!
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <ctype.h>
    #include <string.h>
    
    int validate_email(char* mailaddr)
    {
          /* allow alpha, digits, @ and underscore - you can modify this to 
              include other permissible characters */
          int i, reply = 1, c;
          
          for( i = 0; i < strlen(mailaddr) && reply; i++ )
          {
                c = mailaddr[ i ];
                if( ! isalpha(c) && ! isdigit(c) && c != '@' && c != '_' )
                      reply = 0;
          }
    
          return reply;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice