How to Validate a Domain Name Using Regex & PHP

How to validate a domain name using Regex & PHP?

How about:

^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$

Regex to validate domain name without top-level domain using preg_match

The answer to the problem is changing the pattern to:

$pattern = "(\A[a-zA-Z0-9](?:[a-zA-Z0-9\d-]*[a-zA-Z0-9])+\z)";

Which doesn't allow hyphens at the beginning or the end of the domain, doesn't allow any TLDs, and any other special characters.

Validate a domain name with GET parameters using a REGEX

This code is not tested, but I think it should work:

$pattern = "([a-z0-9-.]*)\.([a-z]{2,3})"; //Host
$pattern .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; //Get requests
if (preg_match($pattern, 'domain.com?test=test')) {
echo 'true';
} else {
echo 'false';
}

What is a regular expression which will match a valid domain name without a subdomain?

Well, it's pretty straightforward a little sneakier than it looks (see comments), given your specific requirements:

/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/

But note this will reject a lot of valid domains.

php preg_match validate domain

 if (preg_match("!"#$%&\'()*+,-./@:;<=>[\\]^_`{|}~", $myString)) 
{
//valid url
}

This should do...

Regex for specific domain name

Now instead of using regex you can simply use strstr function of PHP like as

$email = "xyz@xyz.com";
$email2 = "xyz@xyz.net";
$valid_domain = "@xyz.com";

function checkValidDomain($email, $valid_domain){
if(!filter_var($email,FILTER_VALIDATE_EMAIL) !== false){
if(strstr($email,"@") == $valid_domain){
return "Valid";
}else{
return "Invalid";
}
}else{
return "Invalid Email";
}
}

echo checkValidDomain($email, $valid_domain);// Valid
echo checkValidDomain($email2, $valid_domain);// Invalid

Why I didn't used regex over here you can read many of those threads on SO too Email validation using regular expression in PHP and Using a regular expression to validate an email address

Php regular expression, url validation, following dots in domain name is valid?

You can use this version of your regex:

^(http:[\/]{2})((?![^\/]*?\.{2}[^\/]*?)[\w\d\-\_\.]+)(\/(?:[\/\w]+)?)?$

The problem was with the \W (together with \w in the same character class) that matched everything, even a new line.

Regular expression required for valid domain name check in PHP

I guess this is what youre searching for:

^[a-zA-Z0-9]+([a-zA-Z0-9-.]+)?.(com|org|net|mil|edu|de|COM|ORG|NET|MIL|EDU|DE)$



Related Topics



Leave a reply



Submit