[ fromfile: validation.xml id: regexpvalid ]
The class QRegExpValidator uses a QRegExp to validate an input string.
Example 14.7. src/validate/regexval/rinputform.h
The program takes a phone number from the user and displays it only if it is valid.
Example 14.8. src/validate/regexval/rinputform.cpp
[ . . . . ] QRegExp RinputForm::s_PhoneFormat( "(\\+?1[- ]?)?\\(?(\\d{3,3})\\)?[\\s-]?(\\d{3,3})[\\s-]?(\\d{4,4})"); RinputForm::RinputForm(QWidget* parent) : QWidget(parent), m_PhoneEntry(new QLineEdit), m_PhoneResult(new QLabel) { setupForm(); move(500, 500); } void RinputForm::setupForm() { [ . . . . ] QRegExpValidator* phoneValid(new QRegExpValidator(s_PhoneFormat, this)); m_PhoneEntry->setValidator(phoneValid); connect(m_PhoneEntry, SIGNAL(returnPressed()), this, SLOT(computeResult())); } void RinputForm::computeResult() { m_Phone = m_PhoneEntry->text(); if (s_PhoneFormat.exactMatch(m_Phone)) { QString areacode = s_PhoneFormat.cap(2); QString exchange = s_PhoneFormat.cap(3); QString number = s_PhoneFormat.cap(4); m_PhoneResult->setText(QString("(US/Canada) +1 %1-%2-%3") .arg(areacode).arg(exchange).arg(number)); } } [ . . . . ]
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |