diff --git a/libs/ui/include/psemek/ui/edit.hpp b/libs/ui/include/psemek/ui/edit.hpp index ad54c6f4..4374dd37 100644 --- a/libs/ui/include/psemek/ui/edit.hpp +++ b/libs/ui/include/psemek/ui/edit.hpp @@ -46,8 +46,8 @@ namespace psemek::ui using validator_type = std::function; virtual bool set_validator(validator_type validator); - static validator_type numeric_nonnegative(); - static validator_type numeric(); + static validator_type numeric_nonnegative(int max_length); + static validator_type numeric(int max_length); using callback_type = std::function; virtual void on_text_entered(callback_type callback, bool signal = true); diff --git a/libs/ui/source/edit.cpp b/libs/ui/source/edit.cpp index fb7188f0..9ce87857 100644 --- a/libs/ui/source/edit.cpp +++ b/libs/ui/source/edit.cpp @@ -63,22 +63,22 @@ namespace psemek::ui return true; } - edit::validator_type edit::numeric() + edit::validator_type edit::numeric(int max_length) { - return [](std::u32string_view const str) -> bool { + return [max_length](std::u32string_view const str) -> bool { auto begin = str.begin(); auto end = str.end(); if (begin != end && *begin == '-') ++begin; - return std::all_of(begin, end, [](char32_t c){ return '0' <= c && c <= '9'; }); + return std::all_of(begin, end, [](char32_t c){ return '0' <= c && c <= '9'; }) && (end - begin) <= max_length; }; } - edit::validator_type edit::numeric_nonnegative() + edit::validator_type edit::numeric_nonnegative(int max_length) { - return [](std::u32string_view const str) -> bool { - return std::all_of(str.begin(), str.end(), [](char32_t c){ return '0' <= c && c <= '9'; }); + return [max_length](std::u32string_view const str) -> bool { + return std::all_of(str.begin(), str.end(), [](char32_t c){ return '0' <= c && c <= '9'; }) && str.length() <= max_length; }; }