Aa simple string validator class in Python for basic data validation such as checking if a string is alpha, alphanumeric, e-mail etc.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
stringvalidator.py

README.md

stringvalidator.py

Aa simple string validator class in Python for basic data validation such as checking if a string is alpha, alphanumeric, e-mail etc.

Kailash Nadh, March 2013

License: MIT License

Documentation: http://nadh.in/code/stringvalidator.py

Validation checks

Check Description Arguments
is_numeric Check if a string is numeric (123, 12.3, -12.3 etc.)
is_alpha Check if a string is alphabetical (a-z only)
is_alphanumeric Check if a string is alphanumeric (a-z and 0-9)
is_integer Check if a string is an integer (1, -1, 0, but not 1.1)
is_float Check if a string is a float (1.0, -2.0, but not 1)
longer_than Check if a string is longer than a given length (length)
shorter_than Check if a string is shorter than a given length (length)
is_email Check if a given string is a valid e-mail
is_tld Check if a given string is a top level domain (www.site.com, site.com, but not a.b.site.com)
is_handle Check if a given string is a valid username handle containing only alphanumeric and _ (username, username9, user_name, 1user ...)

Usage

Any number of conditions can be chained and passed to the StringValidator.validate() method.

StringValidator.validate(input, [checks], log)

  • input is the input string to be validated. It is always stripped of preceding and trailing spaces

  • checks is the chained list of checks. Checks with arguments are passed as tuples

  • log is an optional boolean flag. If set to False(default), StringValidator.validate() simply returns True, False if all checks pass, or one of the checks fail, respectively. If set to False, True is returned if all checks pass, or a dict containing what failed and passed is returned. Example:

{
	'not_empty': True,
	'is_email': False
}

Examples

from StringValidator import StringValidator

validator = StringValidator()

input = "user@email.com"
validator.validate(input, ['is_email'])
# => True

input = "user@email.com"
validator.validate(input, ['is_email', ('longer_than', 20)])
# => False

input = "123.4"
validator.validate(input, ['not_empty', ('shorter_than', 5), 'is_float'])
# => True

input = "123.4"
validator.validate(input, ['is_float', ('longer_than', 5), ('shorter_than', 10)])
# => True


input = "123"
validator.validate(input, ['not_empty', 'is_alpha'], True)
# => {
	'not_empty': True,
	'is_alpha': False
}