Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi..
I am doing validation of email id, output is coming wrong, when I give email id without "@" also the "valid" message box appears. Don't know whats going wrong,, pls.. help me
C++
void CEMailDlg::OnValidate()
{
    // TODO: Add your control notification handler code here
    CString m_sFrom;
    m_sFrom=IDC_EDIT_FROM;

    for(int a=0;a < m_sFrom.GetLength(); a++)
    {
        if(m_sFrom.GetAt(a)!='@';)
        {
        AfxMessageBox("valid id");
        }
        else
        {
        AfxMessageBox("invalid id");
        }
    }
}

Thank you.
Posted
Updated 12-May-11 4:04am
v3

In your code you must eliminate the ! from the comparison... you are checking if the value is different from @ and showing a VALID message...

You can use...

CString m_sFrom;
GetdlgItemText(IDC_EDIT_FROM, m_sFrom);  // here you are getting the right text...

if (m_sFrom.Find("@")!=-1)
{
  AfxMessageBox("valid");
}
else
{
  AfxMessageBox("Invalid");
}


You could do DDX which is easier to interact...

HTH...

PS: you should follow the advice from John... regex is the way to go... I've only followed your sample correcting the way you are checking the contents on the string and a couple of things more...
 
Share this answer
 
v2
There are several regex solutions for validating email addresses. All you need is a little google foo.
 
Share this answer
 
Comments
Joan M 12-May-11 9:32am    
my 5... and reference from my answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900