How can I create a filter for unavailable urls? (javascript)

Asked

Viewed 43 times

0

Hello, I was wondering how I can filter certain sub-domains that would simulate my company’s brand for example: in a free hosting the user types a Sub-domain to the address of his website. if he typed a domain as just the word "blog" or "forum", "news". a notice would appear stating that this domain is invalid. i would like a client-side script, and only to avoid that users create sites simulating my company’s brand as for example: he could not create blog.meudominio.xy, filter these words, but when typed "blog21.meudominio.xy" do not appear this warning.

**Exemplo**

1 answer

1


You can create an array on js and use the method indexOf. Ex:

const subdomainDisabled = ["blog", "news", "cdn"];
const input = document.querySelector("input");
const btn = document.querySelector("button");

btn.addEventListener("click", function() {
  if (subdomainDisabled.indexOf(input.value) >= 0) {
    alert("Choose another subdomain");
    input.value = "";
  } else {
    $('#ID-DO-MODAL').modal("show");
  }
});
<input type="text" id="subdomain" />
<button type="button">Register</button>

  • Thank you so much was just missing this detail to finish the project! but again I analyzed the code, I use a modal bootstrap to say that everything went well could you tell me how to make a condition? if the domain is invalid do not display the modal and display the warning Alert, if all right, display the modal.

  • @Hellfiveosborn, I modified my answer. Just replace ID-DO-MODAL for ID of your modal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.