I am working on a DNS panel in PHP and I have to validate a DNS record name without the trailing dot. Here are a few examples:
example.com - match
sub.example.com - match
sub.sub.example.com - match
*.example.com - match
*.sub.example.com - match
sub.*.example.com - no mach
sub*.example.com - no match
*sub.example.com - no match
I am currently using this regex but the problem is it won't match a wildcard (*):
^(?!\-)(?:[a-z\d\-]{0,62}[a-z\d]\.){1,126}(?!\d+)[a-z\d]{1,63}$
I am not so good in formating regex. What is the best way to achieve this? Thanks!
I found a solution using regex that doesn't fully respect domain rules but it works good enough, so if you are planning on using it I would suggest to do additional checks:
^(\*\.)?([a-z\d][a-z\d-]*[a-z\d]\.)+[a-z]+$
There is a better way of solving this through PHP:
$tmp = $domain_to_check;
if(strpos($tmp, '*.') === 0){
$tmp = substr($tmp, 2);
}
if(filter_var('http://' . $tmp, FILTER_VALIDATE_URL)){
// The format is valid
}else{
// The format is invalid
}