Categories
JavaScript Answers

How to check confirm password field in form without reloading page with JavaScript?

Sometimes, we want to check confirm password field in form without reloading page with JavaScript.

In this article, we’ll look at how to check confirm password field in form without reloading page with JavaScript.

How to check confirm password field in form without reloading page with JavaScript?

To check confirm password field in form without reloading page with JavaScript, we can check the input values.

For instance, we write

<label
  >password :
  <input name="password" id="password" type="password" />
</label>
<br />
<label
  >confirm password:
  <input type="password" name="confirm_password" id="confirm_password" />
  <span id="message"></span>
</label>

to add the password inputs.

Then we write

const check = () => {
  if (
    document.getElementById("password").value ===
    document.getElementById("confirm_password").value
  ) {
    document.getElementById("message").style.color = "green";
    document.getElementById("message").innerHTML = "matching";
  } else {
    document.getElementById("message").style.color = "red";
    document.getElementById("message").innerHTML = "not matching";
  }
};

const passwordInput = document.querySelector("#password");
const confirmPasswordInput = document.querySelector("#confirm_password");
passwordInput.onkeyup = check;
passwordInput.confirmPasswordInput = check;

to define the check function.

In it, we select the inputs with getElementById.

Then we get the value from each input with the value property.

And then we set the color and text content of the span according to whether the 2 values match or not.

Then we select the inputs and set their onkeyup property to the check function to call check as we type into the inputs.

Conclusion

To check confirm password field in form without reloading page with JavaScript, we can check the input values.

Categories
JavaScript Answers

How to check if string contains Latin characters only with JavaScript?

Sometimes, we want to check if string contains Latin characters only with JavaScript.

In this article, we’ll look at how to check if string contains Latin characters only with JavaScript.

How to check if string contains Latin characters only with JavaScript?

To check if string contains Latin characters only with JavaScript, we use a regex.

For instance, we write

if (str.match(/[a-z]/i)) {
  // ...
}

to check if string str has only a to z in a case insensitive manner.

We call match to find any matches with this pattern.

Conclusion

To check if string contains Latin characters only with JavaScript, we use a regex.

Categories
JavaScript Answers

How to loop through an array without using array size with Node.js and JavaScript?

Sometimes, we want to loop through an array without using array size with Node.js and JavaScript.

In this article, we’ll look at how to loop through an array without using array size with Node.js and JavaScript.

How to loop through an array without using array size with Node.js and JavaScript?

To loop through an array without using array size with Node.js and JavaScript, we use the forEach method.

For instance, we write

const myArray = ["1", "2", 3, 4];

myArray.forEach((value) => {
  console.log(value);
});

to call myArray.forEach with a callback to loop through element.

We get the element being looped through with value.

Conclusion

To loop through an array without using array size with Node.js and JavaScript, we use the forEach method.

Categories
JavaScript Answers

How to add target=”_blank” to a link within a specified div with JavaScript?

Sometimes, we want to add target="_blank" to a link within a specified div with JavaScript.

In this article, we’ll look at how to add target="_blank" to a link within a specified div with JavaScript.

How to add target="_blank" to a link within a specified div with JavaScript?

To add target="_blank" to a link within a specified div with JavaScript, we use querySelectorAll.

For instance, we write

const linkList = document.querySelectorAll("#link_other a");

for (const link of linkList) {
  link.setAttribute("target", "_blank");
}

to call querySelectorAll to select all the links inside the element with ID link_other.

Then we use a for-of loop to loop through each link.

In it, we call setAttribute to set the 'target' to '_blank'.

Conclusion

To add target="_blank" to a link within a specified div with JavaScript, we use querySelectorAll.

Categories
JavaScript Answers

How to get the base URL in JavaScript?

Sometimes, we want to get the base URL in JavaScript.

In this article, we’ll look at how to get the base URL in JavaScript.

How to get the base URL in JavaScript?

To get the base URL in JavaScript, we use the window.location.origin and window.location.host properties.

For instance, we write

const baseUrl = window.location.origin;
const host = window.location.host;

to get the base URL with the protocol with window.location.origin.

To just get the host name, we use the window.location.host property.

Conclusio

To get the base URL in JavaScript, we use the window.location.origin and window.location.host properties.