PDA

View Full Version : Validate e-mail with QRegularExpression



guidupas
13th February 2015, 19:30
Hello!

I am having a problem to validate an e-mail with QRegularExpression.

I am trying with me@me.com an it returns false



bool regExpressionRetorno::validaEmail(QString email)
{
bool retorno = true;

qDebug() << email;

QRegularExpression regex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");

if(!regex.match(email).hasMatch())
{
retorno = false;
}

return retorno;
}


Whats is going wrong?

Thanks in advance.

ChrisW67
13th February 2015, 20:50
Your regular expression will only match uppercase letters. Reread the description of this RE http://www.regular-expressions.info/email.html. Take a look at the other parameters to the QRegularExpression constructor.

guidupas
13th February 2015, 20:53
Solved



QRegularExpression regex("^[0-9a-zA-Z]+([0-9a-zA-Z]*[-._+])*[0-9a-zA-Z]+@[0-9a-zA-Z]+([-.][0-9a-zA-Z]+)*([0-9a-zA-Z]*[.])[a-zA-Z]{2,6}$");


Thanks for the reply ChrisW67