2

I used this code for validating email id , im getting few errors i dono how to solve it,,, im new to MFC,, if im silly pls forgive me

BOOL CMailDlg::Validate(CString m_sFrom)
{
  m_sFrom  = NulltoString(m_sFrom);
  CString strRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  

  Regex re = new Regex(strRegex);
  if (re.IsMatch(m_sFrom))
    return (true);
  else
    return (false);
}

Errors:

error C2511: 'Validate' : overloaded member function 'int (class CString)' not found in 'CMailDlg'

see declaration of 'CMailDlg'

error C2059: syntax error : 'bad suffix on number'

error C2018: unknown character '0x40'

error C2017: illegal escape sequence

Community
  • 1
  • 1
Hemanth
  • 413
  • 7
  • 17
  • Erm. Why did you tag this `vb6`? What part of MFC has anything to do with VB 6? – Cody Gray - on strike May 09 '11 at 08:34
  • 1
    [This](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html) is the regular expression that you need to do this right. Validating email addresses using regular expressions is surprisingly hard, and thus is definitely **not** the right way to do it. [The answers to this question](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) reach a similar consensus. There are lots of characters allowed in an email address beyond alphanumerics and the `@` sign. – Cody Gray - on strike May 09 '11 at 08:37

1 Answers1

1

You will need to include the regex string in quotes and escape the \. C++ doesn't have native support for regex as you might find is say Perl, it is implemented using a string. \ is the C++ escape character and used to include things like new lines into strings, as such if you want an actual \ in your string you must double it up.

CString strRegex = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/";
Charles Keepax
  • 2,392
  • 1
  • 18
  • 19
  • Is the regex really wrapped in `/`-es? I wouldn't think so. – Jan Hudec May 09 '11 at 09:42
  • Afraid I haven't used the regular expression library in question so can't say, however it would be in Perl and Vim which are the two places I generally use regular expressions. – Charles Keepax May 09 '11 at 09:48