-1

How can i validate domains separated by comma, like site.com,site2.com,sub.site3.com which are coming from a textarea return true or alse in PHP. thanks

if($_POST['domains'] != '')
{
//validation here
}
else
{
$check ="1";
    echo "Error, You didnt enter a domain name!";
}
wmsgeek
  • 7
  • 3

1 Answers1

0

Using a function from this answer:

// returns true if $domain_name is valid; false otherwise
function is_valid_domain_name($domain_name) {
    return filter_var('abc@' . $domain_name, FILTER_VALIDATE_EMAIL) !== false;
}

// make sure $_POST['domains'] exists and isn't empty
if(isset($_POST['domains']) && $_POST['domains'] != '') {

    // split the domains around commas
    $domains = explode(',', $_POST['domains']);

    foreach($domains as $domain) {

        // remove white space from both ends of the string
        $domain = trim($domain);

        // validate the domain; if it fails, output an error but continue with the rest
        if(!is_valid_domain_name($domain)) {
            echo $domain, ' is not a valid domain name!', "\n";
        }
    }
}
else {
    $check = 1;
    echo 'Error, You didn\'t enter a domain name!';
}
Community
  • 1
  • 1
George Brighton
  • 5,131
  • 9
  • 27
  • 36