Categories
JavaScript Answers

How to manually show a HTML5 validation message from a JavaScript function?

Sometimes, we want to manually show a HTML5 validation message from a JavaScript function.

In this article, we’ll look at how to manually show a HTML5 validation message from a JavaScript function.

How to manually show a HTML5 validation message from a JavaScript function?

To manually show a HTML5 validation message from a JavaScript function, we can call the reportValidity method.

For instance, we write:

<form>
  <input type="email" id="email" placeholder="email" required>
  <button type="submit">Submit</button>
</form>

to add a form.

Then we write:

const form = document.querySelector("form");
if (form.checkValidity()) {
  form.submit();
} else {
  form.reportValidity();
}

We select the form with document.querySelector.

Then we call checkValidity to validate the form values.

Then we call reportValidity when there are invalid form values.

As a result, we should see form validation messages.

Conclusion

To manually show a HTML5 validation message from a JavaScript function, we can call the reportValidity method.

Categories
JavaScript Answers

How to round down number 2 decimal places with JavaScript?

Sometimes, we want to round down number 2 decimal places with JavaScript.

In this article, we’ll look at how to round down number 2 decimal places with JavaScript.

How to round down number 2 decimal places with JavaScript?

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

For instance, we write:

const n = Math.floor(1.5675675 * 100) / 100
console.log(n)

We want to round 1.5675675 down to 2 decimal places.

To do this, we multiply 1.5675675 by 100.

Then we call Math.floor on the product to take the floor.

And then we divided that by 100.

Therefore, n is 1.56.

Conclusion

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

Categories
JavaScript Answers

How to add members to an existing object with JavaScript?

Sometimes, we want to add members to an existing object with JavaScript.

In this article, we’ll look at how to add members to an existing object with JavaScript.

How to add members to an existing object with JavaScript?

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

For instance, we write:

const obj = {
  fn1: () => {}
}
obj.fn2 = () => {};
console.log(obj)

We set obj.fn2 to a function to add fn2 as a property of obj.

Conclusion

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

Categories
JavaScript Answers

How to convert integers to hex string in JavaScript?

Sometimes, we want to convert integers to hex string in JavaScript.

In this article, we’ll look at how to convert integers to hex string in JavaScript.

How to convert integers to hex string in JavaScript?

To convert integers to hex string in JavaScript, we can use the number’s toString method.

For instance, we write:

const g = 255;
const gPadded = Number(g).toString(16).padStart(2, "0");
console.log(gPadded)

to convert g to a number with Number.

Then we call toString with 16 to convert the number to a hex string.

Finally, we call padStart with length 2 and string '0' to pad the string to 2 characters with 0’s at the start of the string.

So gPadded is 'ff'.

Conclusion

To convert integers to hex string in JavaScript, we can use the number’s toString method.

Categories
JavaScript Answers

How to get the UNIX timestamp for the start of today in JavaScript?

Sometimes, we want to get the UNIX timestamp for the start of today in JavaScript.

In this article, we’ll look at how to get the UNIX timestamp for the start of today in JavaScript.

How to get the UNIX timestamp for the start of today in JavaScript?

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.

For instance, we write:

const now = new Date();
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const timestamp = startOfDay / 1000;
console.log(timestamp)

to create a new Date instance with the current date and time.

Then we create another Date instance with the year, month and day of the month of today and assigned it to startOfDay.

This will set the time to midnight of the same day.

Then we divide startOfDay by 1000 to return the UNIX timestamp.

Conclusion

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.