Check for valid domain name in a string?

17,390

The source of the validators module shows, that this is maybe a little more complex task.

You could use that module:

>>> import validators
>>> validators.domain('example.com')
True

>>> validators.domain('example.com/')
ValidationFailure(func=domain, ...)

Or you can use the RFCs for Domain names to construct your own checker.

Share:
17,390

Related videos on Youtube

Vipul Gangwar
Author by

Vipul Gangwar

Updated on September 16, 2022

Comments

  • Vipul Gangwar
    Vipul Gangwar over 1 year

    I am using python and would like a simple regex to check for a domain name's validity. I check at least write domain name.

    url = 'https://stackoverflow'
            keyword = 'foo'
            with self.assertRaises(ValueError):
                check_keyword(url, keyword)
    

    I try unit testing on url textfield and there is main.py page where I done the validation main.py-

    def check_keyword(url, keyword):

    if re.match("^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$" ,url):
       return ValueError("Invalid")
    

    Example

  • user3289695
    user3289695 over 6 years
    I am using that same module and the regular expression that it uses looks weird. Also, it gives this domain as valid: "a.aa*com" and I think it shouldn't be, specially because it has got an asterisk as a part of it.
  • Akash Wankhede
    Akash Wankhede almost 4 years
    validators.domain("example.com") cannot check for subdomains. So, It wiil return True for validators.domain("abc.example.com") also.
  • Nemo
    Nemo almost 4 years
    The validators package marks domains with underscores or trailing dots as invalid.