Skip to content

Allows number verification on MDTextfield#1841

Open
Jo-e7 wants to merge 1 commit intokivymd:masterfrom
Jo-e7:master
Open

Allows number verification on MDTextfield#1841
Jo-e7 wants to merge 1 commit intokivymd:masterfrom
Jo-e7:master

Conversation

@Jo-e7
Copy link

@Jo-e7 Jo-e7 commented Dec 26, 2025

This adds number verification functionality to MDTextField. Here is a demo of the functionality:

number_validator.mp4

The code used for the demo was:

from kivy.lang import Builder

from kivymd.app import MDApp

KV = '''
MDScreen:
    md_bg_color: app.theme_cls.surfaceColor

    MDTextField:
        mode: "outlined"
        size_hint_x: None
        pos_hint: {"center_x": .5, "center_y": .5}
        width: "240dp"
        validator: "number"
        MDTextFieldLeadingIcon:
            icon: "numeric"
        MDTextFieldHintText:
            text: "Insert a number"

'''


class Number_validator(MDApp):
    def build(self):
        return Builder.load_string(KV)


Number_validator().run()

@HeaTTheatR
Copy link
Member

The code contains several problems:

  • A logic error: the function returns True on the first non-digit, and False only if all characters are digits. This is the opposite of the typical behavior of is_number_valid (which usually returns True for valid numbers).
  • An redundant break: the return already breaks the loop.
  • An incorrect name: the function returns True on an error.

Easier:

def is_number_valid(self, text: str) -> bool:
    """Checks if the text contains only digits."""

    return text.isdigit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants