Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Match for Domain Name in Django Model

I have one table, which looks like:

class Tld(models.Model):
    domainNm = models.CharField(validators=[ RegexValidator('^[0-9]^[a-z]','yourdomain.com only','Invalid Entry')], max_length=40)
    dtCreated = models.DateField()

for domainNm - I want to validate on any entry that looks like:

  • domain.com
  • 1domain.com
  • domain1.com

It has to follow this way : <domainname>.[com|biz|net] etc and be alphanumeric.

How do I do this on the model level of a django model?

Thanks

like image 355
CodeTalk Avatar asked Jul 23 '13 21:07

CodeTalk


2 Answers

If you want to validate HTTP URL's, forget the regex and use the builtin validator.

If you want only domains without any protocol, try:

def full_domain_validator(hostname):
    """
    Fully validates a domain name as compilant with the standard rules:
        - Composed of series of labels concatenated with dots, as are all domain names.
        - Each label must be between 1 and 63 characters long.
        - The entire hostname (including the delimiting dots) has a maximum of 255 characters.
        - Only characters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9'.
        - Labels can't start or end with a hyphen.
    """
    HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
    if not hostname:
        return
    if len(hostname) > 255:
        raise ValidationError(_("The domain name cannot be composed of more than 255 characters."))
    if hostname[-1:] == ".":
        hostname = hostname[:-1]  # strip exactly one dot from the right, if present
    for label in hostname.split("."):
        if len(label) > 63:
            raise ValidationError(
                _("The label '%(label)s' is too long (maximum is 63 characters).") % {'label': label})
        if not HOSTNAME_LABEL_PATTERN.match(label):
            raise ValidationError(_("Unallowed characters in label '%(label)s'.") % {'label': label})

Usage:

from django.core.validators import URLValidator

field = models.CharField(_('host name'), max_length=255, validators=[URLValidator])

or

field = models.CharField(_('host name'), max_length=255, validators=[full_domain_validator])
like image 61
Adrián Avatar answered Sep 23 '22 03:09

Adrián


To recap the clarifications above: You want to match only domains with a single alphanumeric label and a TLD of up to 4 characters, eg. "domain.com" or "someotherdomain.info" or "345xyz.pdq1" but not "subdomain.domain.com", "http://domain.com", "www.domain.com", or "345xyz.abcde". This regex will do it:

^[a-z0-9]+\.[a-z0-9]{1,4}$
like image 41
Adi Inbar Avatar answered Sep 26 '22 03:09

Adi Inbar