Last active
November 24, 2015 02:26
-
-
Save snowleung/e211d35e832e851c7141 to your computer and use it in GitHub Desktop.
django form validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
from django import forms | |
from django.core.validators import RegexValidator | |
from datetime import datetime | |
import re | |
def only_character_number_underline(value): | |
RegexValidator(regex=r'^[A-Za-z0-9_]+$', message='only number, A-Z, a-z, _')(value) | |
def only_character_number(value): | |
RegexValidator(regex=r'^[A-Za-z0-9]+$', message='only number, A-Z, a-z')(value) | |
def only_32_hexadecimal_characters(value): | |
RegexValidator(regex=re.compile('[0-9a-f]{32}\Z', re.I), message='not exactly 32 hexadecimal characters')(value) | |
def validate_date(value): | |
try: | |
datetime.strptime(value, '%Y-%m-%d') | |
except Exception as ex: | |
raise forms.ValidationError(ex) | |
def validate_iso_date(value): | |
validate_date(value) | |
d = datetime.strptime(value, '%Y-%m-%d') | |
if str(value) != d.date().isoformat(): | |
raise forms.ValidationError('date not iso format date') | |
def validate_week_U(value): | |
''' | |
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. | |
''' | |
try: | |
datetime.strptime(value, '%Y-%U') | |
except Exception as ex: | |
raise forms.ValidationError(ex) | |
def validate_week_perfix_for_zero(value): | |
validate_week_U(value) | |
try: | |
_ww = str(value)[5:] # yyyy-ww ==> ww | |
if int(_ww) < 10 and _ww[0] != '0': | |
raise forms.ValidationError('week perfix for zero, {} ==> {}-0{}'.format(value, value[:4], _ww)) | |
except Exception as ex: | |
raise forms.ValidationError(ex) | |
def validate_month(value): | |
''' | |
Month as a zero-padded decimal number. | |
Example: 01, 02, ..., 12 | |
''' | |
try: | |
datetime.strptime(value, '%Y-%m') | |
except Exception as ex: | |
raise forms.ValidationError(ex) | |
def validate_month_prefix_for_zero(value): | |
''' | |
Month as a zero-padded decimal number. | |
Example: 01, 02, ..., 12 | |
''' | |
validate_month(value) | |
try: | |
_mm = str(value)[5:] # yyyy-mm ==> mm | |
if int(_mm) < 10 and _mm[0] != '0': | |
raise forms.ValidationError('month perfix for zero, {} ==> {}-0{}'.format(value, value[:4], _mm)) | |
except Exception as ex: | |
raise forms.ValidationError(ex) | |
def validate_period(value): | |
_p = str(value) | |
if _p not in ('day', 'week', 'month'): | |
raise forms.ValidationError('''period not in ('day', 'week', 'month')''') | |
def validate_key(value): | |
_k = str(value) | |
if _k not in ('J', 'W'): | |
raise forms.ValidationError('''key not in ('J', 'W')''') | |
def validate_unit(value): | |
_u = str(value) | |
if _u not in ('DAYS', 'MONTHS'): | |
raise forms.ValidationError('''unit not in ('DAYS', 'MONTHS')''') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
regex reference , http://blog.sina.com.cn/s/blog_887d00920100tvvd.html