user3101664 user3101664 - 24 days ago 9
jQuery Question

domain validate in php using preg_match

i want to validate the domain name in php using preg_match

For example these are all valid:

https://www.google.com
http://www.google.com
www.google.com
google.com
google.com/
https://www.google.com.tr/search?dcr=0&source=hp&q=web+ui&


i can validate following example only

https://www.google.com
http://www.google.com
www.google.com
google.com

$domain_validation = "/(?:http(s)?:\/\/)?(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{2,63}|[a-z0-9]{2,59})?(?:\/)+$/i";
if(preg_match("$domain_validation", $inputDomainName)){
$validSite = true;
}


and i want to validate this

google.com/
https://www.google.com.tr/search?dcr=0&source=hp&q=web+ui&


Please help to solve

Answer Source

Try this one. It should do exactly what you want.

$domain_validation = '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/';
if(preg_match("$domain_validation", $inputDomainName)){
    $validSite = true;
}