validate data entry
-
Hi guys, I'm doing tests for the validation of the data entry, I'm using Pyside2, I'm trying to validate the full name of a person, I've tried to do it with regular expressions but the truth is that I have not achieved anything good, here is my code to see if you can give me any suggestions on how I can solve my problem.
code:
iimport re from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * class Ui_Dialog(QDialog): def setupUi(self, Dialog): if not Dialog.objectName(): Dialog.setObjectName(u"Dialog") Dialog.resize(312, 71) Dialog.setWindowFlags(Qt.Dialog|Qt.MSWindowsFixedSizeDialogHint) self.verticalLayout = QVBoxLayout(Dialog) self.verticalLayout.setObjectName(u"verticalLayout") self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") self.label = QLabel(Dialog) self.label.setObjectName(u"label") self.horizontalLayout.addWidget(self.label) self.txtNombres = QLineEdit(Dialog) self.txtNombres.setObjectName(u"txtNombres") self.horizontalLayout.addWidget(self.txtNombres) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QHBoxLayout() self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") #self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) #self.horizontalLayout_2.addItem(self.horizontalSpacer) self.horizontalLayout_2.addStretch() self.btnSalir = QPushButton(Dialog) self.btnSalir.setObjectName(u"btnSalir") self.btnSalir.setIcon(QIcon("img/cancel.png")) self.horizontalLayout_2.addWidget(self.btnSalir) self.btnSalir.clicked.connect(self.cancelar) self.btnEnviar = QPushButton(Dialog) self.btnEnviar.setObjectName(u"btnEnviar") self.btnEnviar.setIcon(QIcon("img/accept.png")) self.horizontalLayout_2.addWidget(self.btnEnviar) self.btnEnviar.clicked.connect(self.enviar) self.verticalLayout.addLayout(self.horizontalLayout_2) self.retranslateUi(Dialog) QMetaObject.connectSlotsByName(Dialog) # setupUi def retranslateUi(self, Dialog): Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None)) self.label.setText(QCoreApplication.translate("Dialog", u"Nombres:", None)) self.btnSalir.setText(QCoreApplication.translate("Dialog", u"Cancelar", None)) self.btnEnviar.setText(QCoreApplication.translate("Dialog", u"Enviar", None)) # retranslateUi def enviar(self): #self.nombre=" ".join(self.txtNombres.text().split()) if not self.nomIsValid(self.txtNombres.text()): QMessageBox.warning(self,qApp.applicationName(),"Datos incorrectos.") self.txtNombres.selectAll() self.txtNombres.setFocus() return QMessageBox.information(self,"Mensaje", "Información enviada.\nSus datos: {}".format(self.txtNombres.text())) self.txtNombres.clear() self.txtNombres.setFocus() def nomIsValid(self,nom): ptrn=re.compile(r'^[a-zA-ZñÑ]+') if ptrn.fullmatch(nom) is None: return False return True def cancelar(self): quit()
the main script:
from PySide2.QtWidgets import QApplication, QDialog from Ventana import Ui_Dialog if __name__ == "__main__": import sys app=QApplication(sys.argv) app.setStyle("Fusion") app.setApplicationName("App_test") mWindow=QDialog() ui=Ui_Dialog() ui.setupUi(mWindow) mWindow.show() sys.exit(app.exec_())
I always get the message that the data is incorrect.
-
@lincoln
Firstly do not use the oldQRegExp
class, use the newerQRegularExpression
class for everything, https://doc.qt.io/qt-5/qregularexpression.html.Secondly, I don't know why you are having trouble with an expression for full name. Depends what you want to support, but we could just take your previous expression and allow spaces:
[a-zA-ZñÑ ]+
. Now this is a bit simplistic, because you could just type one or more spaces only and it would match. So we might improve to:[a-zA-ZñÑ][a-zA-ZñÑ ]+
. That is a mandatory letter first, then follow by one or more letters or spaces. And we could continue improving, according to your requirements/definition of a "full name". We could also change thea-zA-ZñÑ
to be a broader category of possible letters, regular expressions have special "character classes".If you want to "play" with regular expressions I suggest you go to somewhere online like https://regex101.com/ where you can develop and test proposed expressions against whatever inputs you expect to accept/reject, till you have something you like. Then bring it back to a Qt
QRegularExpression
. Sometimes these are not 100% identical to what is accepted byQRegularExpression
--- if you use "advanced" constructs --- but mostly it works straight off, or with a minor adjustment. -
ptrn=re.compile(r'^[a-zA-ZñÑ]+')
if ptrn.fullmatch(nom) is None:
Two possibilities:
-
Does your "the full name of a person" mean you are entering forename followed by space and then surname? (Or even leading/trailing spaces in the text edit?) If so you are not matching the space. You should at least show an example of the input your match is failing on!
-
I haven't tested, but because you are using
fullmatch()
, that will already put in the^
anchor for you at the start. Is it treating the^
you have as a literal^
to match at the start? Test it out in Python.
As a completely separate point, are you aware that Qt has https://doc.qt.io/qt-5/qvalidator.html, validators which can be placed on, say,
QLineEdit
to validate as user types? For example, it could disallow the user from even typing a digit into a name. You may or may not wish to use that approach, but you might want to look at it. -
-
ok, thanks I am using QRegExp to validate and the exactMatch function, and it was better for me, and yes, good regarding your comment, well yes, it is the full name, but I have not been able to make the regular expression to validate that field yet, no I have found an expression that meets the pattern for the full name, first and last names, any suggestion of how I could do that validation would be very appreciated, greetings.
-
@lincoln
Firstly do not use the oldQRegExp
class, use the newerQRegularExpression
class for everything, https://doc.qt.io/qt-5/qregularexpression.html.Secondly, I don't know why you are having trouble with an expression for full name. Depends what you want to support, but we could just take your previous expression and allow spaces:
[a-zA-ZñÑ ]+
. Now this is a bit simplistic, because you could just type one or more spaces only and it would match. So we might improve to:[a-zA-ZñÑ][a-zA-ZñÑ ]+
. That is a mandatory letter first, then follow by one or more letters or spaces. And we could continue improving, according to your requirements/definition of a "full name". We could also change thea-zA-ZñÑ
to be a broader category of possible letters, regular expressions have special "character classes".If you want to "play" with regular expressions I suggest you go to somewhere online like https://regex101.com/ where you can develop and test proposed expressions against whatever inputs you expect to accept/reject, till you have something you like. Then bring it back to a Qt
QRegularExpression
. Sometimes these are not 100% identical to what is accepted byQRegularExpression
--- if you use "advanced" constructs --- but mostly it works straight off, or with a minor adjustment. -
@JonB There's no need for duplicating the character class as the + implies at least one element of the expression before it.
@lincoln As for the chars, there might be simpler expression encompassing all what you need. Check this page about unicode. I did not test yet what is available using the PCRE version used by Qt but it's worth checking.
-
@SGaist said in validate data entry:
@JonB There's no need for duplicating the character class as the + implies at least one element of the expression before it.
I don't know what you are referring to. If you mean the
[a-zA-ZñÑ][a-zA-ZñÑ ]+
, that is deliberate! The second[...]
includes a space character. The whole point was:That is a mandatory letter first, then follow by one or more letters or spaces.
Else what are you speaking about?