0

I simply want to validate that a link to help text is a valid link when entering the link in a CRUD operation. When I use fetch it fails if the link is to an invalid site, however, not if it is to a bad page on a site (404). The response from the fetch is the same to both a valid page and a non-vaid page, yet google Chrome seems to know.

DevTools image

  const g_log1 = true
  if (g_log1) console.log('URL ', url)
  try {
    const urlOptions = {
      method: 'HEAD',
      mode: 'no-cors',
      headers: { 'Content-Type': 'application/json' }
    }
    fetch(url, urlOptions)
      .then(response => {
        if (g_log1) console.log(url, '--1--', response)
        return true
      })
      .catch(error => {
        if (g_log1) console.log(url, '--2--', error)
        return false
      })
  } catch (error) {
    if (g_log1) console.log(url, '--3--', error)
    return false
  }
}

export default apiCheckURL
  • You can use regex to validate URLs. See [this thread](https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url). – code Mar 08 '22 at 21:08
  • This validates that the URL has the correct format but not that it is valid. It is a start but not what I am looking for. Thanks. – richard stuart Mar 08 '22 at 23:58
  • So "valid page" as in existing page? You'll need a backend for that. – code Mar 09 '22 at 06:56

0 Answers0