-1

i want to make regex for checking that full FQDN of domain
But i tried pattern like this and not working, its return all to true
I got this pattern from ChatGPT btw

Code:

const isValidFqdn = (fqdn) => {
  const fqdnRegex = /^(?=.{1,253}$)(?!:\/\/)(?!-)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$/,
  
  return fqdnRegex.test(fqdn);
};

Results that i want:

console.log(isValidFqdn('example.com')); // false
console.log(isValidFqdn('example.com.ph')); // false
console.log(isValidFqdn('www.example.com')); // true
console.log(isValidFqdn('www.example.com.ph')); // true
console.log(isValidFqdn('www-images.example.com')); // true
console.log(isValidFqdn('www.example-testing.com')); // true
Comma
  • 35
  • 2
  • 6
  • Does this answer your question? [Fully qualified domain name validation](https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation) – Reyno Aug 18 '23 at 08:18
  • What differentiates between `example.ph.com` and `www.example.com.ph`? Apart from an extra sub-domain of course! You can't count on the number of "parts" as `example.com.ph` and `www.example.com` have the same number. – phuzi Aug 18 '23 at 08:22
  • www.example.com.ph , ph is for the country code @phuzi – Comma Aug 18 '23 at 08:23
  • the link you shared is not work as i want, i want example.com tobe returned as false, only accepting www.example.com etc @Reyno – Comma Aug 18 '23 at 08:24
  • Yes, I know this but that doesn't answer the question. Why isn't `example.com.ph` valid? Why can't that be a valid FQDN? It feels like there's going to be some external logic required that won't fit in to a regular expression. – phuzi Aug 18 '23 at 08:26
  • because i want the subdomain only that be accepted, i dont want main domain to accepted.. @phuzi – Comma Aug 18 '23 at 08:28
  • From a logical point of view though, there's no difference between `com.ph` and `example.com` though. – phuzi Aug 18 '23 at 08:29
  • yes thats right, our production want the system only accepting the subdomain, im so confused for using these regex – Comma Aug 18 '23 at 08:31
  • Ah, so you don't want a general solution for this then. Is it only for a small set of known domains then. – phuzi Aug 18 '23 at 08:32
  • so i cannot using regex to fix this problem? @phuzi – Comma Aug 18 '23 at 08:34
  • 1
    Possibly but it depends on the exact scope of what you're trying to achieve. Can you update (edit) your question to state exactly the task as you have been given it and include exactly what you are having trouble with don't assume that the solution you're attempting is necessarily the correct/best way. It sounds like you don't need a way to validate any possible FQDN though but only a small subset. – phuzi Aug 18 '23 at 08:58
  • let try to only give true value for subdomain only, i want www.example.com is true and example.com is false – Comma Aug 18 '23 at 09:10

0 Answers0