diff --git a/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml b/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml new file mode 100644 index 00000000..45f7b424 --- /dev/null +++ b/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml @@ -0,0 +1,15 @@ +import StatusQ.Controls 0.1 +import StatusQ.Core.Utils 0.1 + +StatusValidator { + name: "addressOrEns" + + errorMessage: "Please enter a valid address or ENS name." + + validate: function (t) { + return Utils.isValidAddress(t) || Utils.isValidEns(t) ? + true : + { actual: t } + } +} + diff --git a/src/StatusQ/Controls/Validators/qmldir b/src/StatusQ/Controls/Validators/qmldir index bd1db88c..0a8d0261 100644 --- a/src/StatusQ/Controls/Validators/qmldir +++ b/src/StatusQ/Controls/Validators/qmldir @@ -1,6 +1,7 @@ module StatusQ.Controls.Validators StatusAddressValidator 0.1 StatusAddressValidator.qml +StatusAddressOrEnsValidator 0.1 StatusAddressValidator.qml StatusValidator 0.1 StatusValidator.qml StatusMinLengthValidator 0.1 StatusMinLengthValidator.qml StatusMaxLengthValidator 0.1 StatusMaxLengthValidator.qml diff --git a/src/StatusQ/Core/Utils/Utils.qml b/src/StatusQ/Core/Utils/Utils.qml index 70de5ac6..2307e4ce 100644 --- a/src/StatusQ/Core/Utils/Utils.qml +++ b/src/StatusQ/Core/Utils/Utils.qml @@ -18,6 +18,14 @@ QtObject { return inputValue !== "0x" && /^0x[a-fA-F0-9]{40}$/.test(inputValue) } + function isValidEns(inputValue) { + if (!inputValue) { + return false + } + const isEmail = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/.test(inputValue) + const isDomain = /(?:(?:(?[\w\-]*)(?:\.))?(?[\w\-]*))\.(?[\w\-]*)/.test(inputValue) + return isEmail || isDomain || (inputValue.startsWith("@") && inputValue.length > 1) + } }