wwfc_barmy_army Posted June 7, 2009 Share Posted June 7, 2009 Hello. I currently use this to check if they have submitted a valid domain: preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain) Works for example google.com but not for example mail.google.com. So if there an easy way to accept subdomains too? Thanks. Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/ Share on other sites More sharing options...
rivan Posted June 7, 2009 Share Posted June 7, 2009 preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z0-9][a-z0-9\-]+[a-z0-9])*(\.[a-z]{2,4})+$/i", $domain) (added first part repeated 0 or more times with dot in front of it) Regards Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/#findComment-850937 Share on other sites More sharing options...
byte1918 Posted June 7, 2009 Share Posted June 7, 2009 Hello. I currently use this to check if they have submitted a valid domain: preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain) Works for example google.com but not for example mail.google.com. So if there an easy way to accept subdomains too? Thanks. i think the problem is that in "mail.google.com" you encounter "." (dot) two times , while your regex is set to check domains which have only one dot. I'm kinda newbie with regular expressions but I will try to make one which works. edit : ah..I hadn't refreshed page, it seems someone has answered your question. sry for useless post. Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/#findComment-850938 Share on other sites More sharing options...
byte1918 Posted June 7, 2009 Share Posted June 7, 2009 Hello. I currently use this to check if they have submitted a valid domain: preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain) Works for example google.com but not for example mail.google.com. So if there an easy way to accept subdomains too? Thanks. I modified rivan regex to: preg_match ("/^[a-z0-9][a-z0-9]*[\-]?+[a-z0-9]*(\.[a-z0-9][a-z0-9\-]+[a-z0-9])*(\.[a-z]{2,4})+$/i", $domain) -because his regex wouldn't match domains starting with one letter before the first dot. (eg. a.google.com) Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/#findComment-850941 Share on other sites More sharing options...
wwfc_barmy_army Posted June 7, 2009 Author Share Posted June 7, 2009 Thanks guys. byte1918's suggestion worked perfectly. Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/#findComment-850944 Share on other sites More sharing options...
Daniel0 Posted June 7, 2009 Share Posted June 7, 2009 Museum is a valid TLD. Just saying. There is also a great deal of valid characters you've missed. Safest thing would probably be to check if there are valid DNS records for a given domain name. If there is then it's obviously valid. checkdnsrr Link to comment https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/#findComment-850947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.