February 3rd, 2011, 06:12 PM
BOOL isValidDomainName("domain name")
My application is perfoming "domain name to ip" and "ip to domain name" resolving.
To reduce request overwok would be useful to check domain name correctness before sending request right in application. To avoid user mistakes in typing domain name.
So we can write a C++ function to check if string is a real domain name or it is a simple string with abracadabra:
function takes Unicode string and return true; if it contains correct domain name, like example.com or return false; if it not.
BOOL isValidDomainName(wchar_t* dName);
How we can implement simple checkup?
Let me think. Valid domain name is differ from ordinary string:
1. domain name can't start with '-' sign
2. domain name can contains only 'a-z' characters, '0-9' digits, and '-' sign.
(No special or control characters).
3. domain name ends with TLD part like .com, .net, .org, .name, .museum and etc.
4. domain name can be multi level like (i.am.correct.com)
I was trying to write my own version of isValidDomainName() but it did not works (
P.S. it's C++ code, not php. Simply I could not find tag for C++ code. Tag for better visual perception.
BOOL isValidDomainName(wchar_t* dName)
wchar_t* p= NULL; //Context pointer to next token
wszTLD=wcstok_s(dName, _T("."), &p);
if(wszTLD==NULL && wcscmp(dName,_T("localhost"))!=NULL) return false; // no dots and no localhost string
wszTLD=wcstok_s(NULL, _T("."), &p);
if(!wcscmp(wszTLD,_T("com"))) return true;
else return false; // there are dots but no com TLD string found
// domain can't start with '-' sign
Maybe your version of this function will be more successful and effective. Any ideas?
Tags for this Thread
Click Here to Expand Forum to Full Width