www.webdeveloper.com
Results 1 to 8 of 8

Thread: regex to validate a domain name

  1. #1
    Join Date
    Sep 2004
    Location
    Sri Lanka
    Posts
    472

    regex to validate a domain name

    Hi,

    Can someone please let me know the regex usable for validating a domain name ?

    it could be

    http://xxx.xxxx.xxx
    https://xxx.xxxx.xxx
    http://xxxx.xxx
    https://xxxx.xxx
    xxxx.xxx
    xxx.xxxx.xxx

    In other words with or without protocols and with root domain names or sub domains, it should validate them.

    Importantly this is for domain name, not to validating a URL which can have folder paths, file names.

    Thank you.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Untested:
    PHP Code:
    $regexp '#^(https?://)?[^/.]+(\.[^/.])+/?$#'
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2004
    Location
    Sri Lanka
    Posts
    472
    hi,

    Thanks for the help, but that regex doesnt help.

    Here are the search conditions

    may or may not contain http:// or https://
    may or may not contain www part
    may or may not contain sub domain part
    must have host part
    there may be trailing / after the domain
    no folder paths or file names

    Best Regards

  4. #4
    Join Date
    Aug 2009
    Posts
    593
    How about this...

    PHP Code:
    function checkDomain($domain)
    {
        if (
    preg_match('/(^(https?):\/\/|^)(([^\.]+)\.([^\.]+\.[^\/$]+)|([^\.]+\.[^\/$]+))\/?$/'$domain$matches)) {

            echo
                
    '<b>' $domain '</b><br />' .
                
    'Protocol: <b>'   . ($matches[2] ? $matches[2] : 'None') . '</b><br />' .
                
    'Sub-Domain: <b>' . ($matches[4] ? $matches[4] : 'None') . '</b><br />' .
                
    'Domain: <b>'     . ($matches[5] ? $matches[5] : $matches[6]) . '</b><br />' .
                
    '<br />';

        } else {
            echo 
    $domain ' - INVALID<br /><br />';
        }
    }

    checkDomain('http://www.domain.com');
    checkDomain('https://www.domain.com/');
    checkDomain('www.domain.com/');
    checkDomain('https://domain.com/file');
    checkDomain('https://domain.com'); 

  5. #5
    Join Date
    Sep 2004
    Location
    Sri Lanka
    Posts
    472
    Hi,

    Thanks it works fine.

    Best Regards

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    You could also look at checkdnsrr.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,432
    Quote Originally Posted by GUIR View Post
    hi,

    Thanks for the help, but that regex doesnt help.

    Here are the search conditions

    may or may not contain http:// or https://
    may or may not contain www part
    may or may not contain sub domain part
    must have host part
    there may be trailing / after the domain
    no folder paths or file names

    Best Regards
    It was missing one "+" character:
    Code:
    '#^(https?://)?[^/.]+(\.[^/.]+)+/?$#'
    Test:
    PHP Code:
    <?php
    $data 
    = array(
       
    'http://xxx.xxxx.xxx',
       
    'https://xxx.xxxx.xxx/',
       
    'http://xxxx.xxx/',
       
    'https://xxxx.xxx',
       
    'xxxx.xxx',
       
    'xxx.xxxx.xxx',
       
    'http://xxx.xxx.xxx/no_good',
       
    'not_a_domain'
    );
    $regexp '#^(https?://)?[^/.]+(\.[^/.]+)+/?$#';
    foreach(
    $data as $test) {
       echo 
    "<p>$test: ";
       echo (
    preg_match($regexp$test)) ? "valid" "NOT valid";
       echo 
    "</p>\n";
    }
    Output:
    Code:
    http://xxx.xxxx.xxx: valid
    
    https://xxx.xxxx.xxx/: valid
    
    http://xxxx.xxx/: valid
    
    https://xxxx.xxx: valid
    
    xxxx.xxx: valid
    
    xxx.xxxx.xxx: valid
    
    http://xxx.xxx.xxx/no_good: NOT valid
    
    not_a_domain: NOT valid
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Sep 2004
    Location
    Sri Lanka
    Posts
    472
    Thank you ..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles