Validators
is_numeric = RegexValidator('^[0-9]*$', 'Only numbers, leading zeros are ok.')
module-attribute
RegexValidator: Validates that a string contains only numeric characters.
This validator ensures that the input string consists solely of digits (0-9). Leading zeros are permitted in the input.
Examples:
Valid inputs: "123", "001", "0", "" Invalid inputs: "12a", "-1", "1.0", "1,000"
Attributes:
| Name | Type | Description |
|---|---|---|
regex |
str
|
The regular expression pattern "^[0-9]*$" |
message |
str
|
The error message displayed when validation fails |
django_validate_email(email, whitelist_domains=None)
Validates an email address using Django's validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
email
|
str
|
Email address to validate. |
required |
whitelist_domains
|
Optional[List[str]]
|
List of allowed email domains. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: The validated email address if valid, None if invalid. |
Examples:
>>> django_validate_email("valid@email.com")
'valid@email.com'
>>> django_validate_email("valid@email.com", whitelist_domains=["email.com"])
'valid@email.com'
>>> print(django_validate_email("valid@email.com", whitelist_domains=["other.com"]))
None
Source code in django_util/validators.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
django_validate_phone(phone, region=None)
Validates and formats a phone number using Django's phone number field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone
|
str
|
Phone number to validate. |
required |
region
|
Optional[str]
|
Region code for parsing local numbers. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: The phone number in E.164 format if valid, None if invalid. |
Examples:
>>> django_validate_phone("+1234567890")
'+1234567890'
>>> print(django_validate_phone("invalid"))
None
Note
Requires django-phonenumber-field package to be installed.
Source code in django_util/validators.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
django_validate_url(url, allowed_schemes=None)
Validates a URL using Django's URL validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to validate. |
required |
allowed_schemes
|
Optional[List[str]]
|
List of allowed URL schemes (e.g., ['http', 'https']). |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: The validated URL if valid, None if invalid. |
Source code in django_util/validators.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |