Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Python Validate that input text only accept letters and numbers with regex


import re

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

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

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

    #return True if the text only contains letters and numbers, False otherwise
    return bool(re.match('^[a-zA-Z0-9]+$', text))

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

Top comments (0)