File indexing completed on 2024-06-16 04:46:58
0001 /* 0002 SPDX-FileCopyrightText: 2013-2015 Christian Dávid <christian-david@web.de> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "ibanvalidator.h" 0007 0008 #include "payeeidentifier/ibanbic/ibanbic.h" 0009 #include <KLocalizedString> 0010 0011 #include "widgetenums.h" 0012 0013 ibanValidator::ibanValidator(QObject* parent) 0014 : QValidator(parent) 0015 { 0016 0017 } 0018 0019 QValidator::State ibanValidator::validate(QString& string, int&) const 0020 { 0021 // Check country code and set it uppercase 0022 if (string.length() >= 1) { 0023 fixup(string); 0024 if (!string.at(0).isLetter()) 0025 return Invalid; 0026 if (string.at(0).isLower()) 0027 string[0] = string.at(0).toUpper(); 0028 } 0029 0030 if (string.length() >= 2) { 0031 if (!string.at(1).isLetterOrNumber()) 0032 return Invalid; 0033 if (string.at(1).isLower()) 0034 string[1] = string.at(1).toUpper(); 0035 } 0036 0037 // Check rest of the iban 0038 int characterCount = qMin(string.length(), 2); 0039 for (int i = 2; i < string.length(); ++i) { 0040 if (string.at(i).isLetterOrNumber()) { 0041 ++characterCount; 0042 } else if (!string.at(i).isSpace()) { 0043 return Invalid; 0044 } 0045 } 0046 0047 if (characterCount > 32) 0048 return Invalid; 0049 0050 if (characterCount > 5) { 0051 return Acceptable; 0052 } 0053 0054 return Intermediate; 0055 } 0056 0057 QPair< eWidgets::ValidationFeedback::MessageType, QString > ibanValidator::validateWithMessage(const QString& string) 0058 { 0059 // string.length() > 32 should not happen because all line edits should have this validator installed 0060 if (string.length() < 5) 0061 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Error, i18n("This IBAN is too short.")); 0062 0063 if (!payeeIdentifiers::ibanBic::validateIbanChecksum(payeeIdentifiers::ibanBic::ibanToElectronic(string))) 0064 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::Warning, i18n("This IBAN is invalid.")); 0065 0066 return QPair< eWidgets::ValidationFeedback::MessageType, QString >(eWidgets::ValidationFeedback::MessageType::None, QString()); 0067 } 0068 0069 void ibanValidator::fixup(QString& string) const 0070 { 0071 string = payeeIdentifiers::ibanBic::ibanToPaperformat(string); 0072 }