Hackerss.com

hackerss
hackerss

Posted on

Python Validate that input text only accept letters and numbers


def validate(text):
    """
    Validate that input text only accept letters and numbers.

    Parameters
    ----------
    text : str
        Text to validate.

    Returns
    -------
    bool
        ``True`` if ``text`` is valid, ``False`` otherwise.
    """

    #check if the text is a string
    if type(text) == str:
        #check if the text only contains letters and numbers
        if text.isalpha() or text.isdigit():
            return True
        else:
            return False
    else:
        return False

#example
validate("abc123")  #True
validate("abc")  #True
validate("abc123!")  #False
validate(123)  #False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)