Categories
JavaScript Answers

How to parse a date in YYYYmmdd format in JavaScript?

Sometimes, we want to parse a date in YYYYmmdd format in JavaScript.

In this article, we’ll look at how to parse a date in YYYYmmdd format in JavaScript.

How to parse a date in YYYYmmdd format in JavaScript?

To parse a date in YYYYmmdd format in JavaScript, we can use some syring methods.

For instance, we write:

const parse = (str) => {
  const y = str.substr(0, 4)
  const m = str.substr(4, 2) - 1
  const d = str.substr(6, 2);
  const date = new Date(y, m, d);
  return (date.getFullYear() === +y && date.getMonth() === +m && date.getDate() === +d) ? date : 'invalid date';
}

console.log(parse('20220202'))

to define the parse function to parse the date string str.

We extract the year y, month m and date d with substr.

And we subtract 1 from m to get a 0-based month required by JavaScript’s Date constructor.

Next, we check if the year, month, and date are all valid with date.getFullYear() === +y && date.getMonth() === +m && date.getDate() === +d.

If that’s true, then we return the date.

Otherwise, we return 'invalid date'.

Conclusion

To parse a date in YYYYmmdd format in JavaScript, we can use some syring methods.

Categories
JavaScript Answers

How to check if a text area is empty in JavaScript?

Sometimes, we want to check if a text area is empty in JavaScript.

In this article, we’ll look at how to check if a text area is empty in JavaScript.

How to check if a text area is empty in JavaScript?

To check if a text area is empty in JavaScript, we can check if e.target.value is falsy or not.

For instance, we write:

<textarea></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector('textarea')
textarea.onchange = (e) => {
  if (!e.target.value) {
    console.log('empty')
  }
}

to select the text area with querySelector.

And we set textarea.onchange to a function that checks if e.target.value is falsy to check if it’s empty when we type into the text area.

Conclusion

To check if a text area is empty in JavaScript, we can check if e.target.value is falsy or not.

Categories
JavaScript Answers

How to get the browser width and height after user resizes the window with JavaScript?

Sometimes, we want to get the browser width and height after user resizes the window with JavaScript.

In this article, we’ll look at how to get the browser width and height after user resizes the window with JavaScript.

How to get the browser width and height after user resizes the window with JavaScript?

To get the browser width and height after user resizes the window with JavaScript, we can get the clientWidth and clientHeight properties from the window resize event handler.

For instance, we write:

window.onresize = () => {
  const {
    clientWidth,
    clientHeight
  } = document.body;
  console.log(clientWidth, clientHeight)
}

We set window.onresize to a function that gets the width and height of the body element with:

const {
  clientWidth,
  clientHeight
} = document.body;

Now when we resize the window, we should see the values logged.

Conclusion

To get the browser width and height after user resizes the window with JavaScript, we can get the clientWidth and clientHeight properties from the window resize event handler.

Categories
JavaScript Answers

How to add a multiple files input and use file reader to preview the selected files with JavaScript?

Sometimes, we want to add a multiple files input and use file reader to preview the selected files with JavaScript.

In this article, we’ll look at how to add a multiple files input and use file reader to preview the selected files with JavaScript.

How to add a multiple files input and use file reader to preview the selected files with JavaScript?

To add a multiple files input and use file reader to preview the selected files with JavaScript, we can loop through each selected file, read them into a data URL, create an image and set the src attribute to the URL, and then append the img element to the DOM.

For instance, we write:

<input type='file' multiple>
<div>

</div>

to add an file input and a div.

Then we write:

const input = document.querySelector('input[type="file"]')
const div = document.querySelector('div')
const picReader = new FileReader();

input.onchange = async (e) => {
  for (const file of e.target.files) {
    await new Promise(resolve => {
      picReader.onload = (e) => {
        const img = new Image()
        img.src = e.target.result
        img.width = 100
        div.appendChild(img)
        resolve()
      }
      picReader.readAsDataURL(file);
    })
  }
}

to select both elements with querySelector.

Then we set the input.onchange property to a function that loops through each selected file with the for-of loop.

In the loop, we create a promise with a callback that sets the picReader.onload property to a funxction that creates a new img element with new Image().

Then we set the src to the data URL that we get from reading the file with:

img.src = e.target.result

And then we call div.appendChild with img to add the img element as the child of the div.

Next, we call resolve to resolve the promise.

Then we call readAsDataURL with file to read the file.

As a result, when we select one or more image files, we can see the selected images on the page.

Conclusion

To add a multiple files input and use file reader to preview the selected files with JavaScript, we can loop through each selected file, read them into a data URL, create an image and set the src attribute to the URL, and then append the img element to the DOM.

Categories
JavaScript Answers

How to validate a file upload field using JavaScript?

Sometimes, we want to validate a file upload field using JavaScript.

In this article, we’ll look at how to validate a file upload field using JavaScript.

How to validate a file upload field using JavaScript?

To validate a file upload field using JavaScript, we can check the value property.

For instance, we write:

<form>
  <input type='file'>
  <input type='submit'>
</form>

to add a form.

Then we write:

const form = document.querySelector('form')
const input = document.querySelector('input[type="file"]')

form.onsubmit = (e) => {
  e.preventDefault()
  if (input.value) {
    console.log('file selected')
  }
}

to select the form and the file input with querySelector.

Then we set the form.onsubmit property to a function that checks if input.value is truthy.

If it is, then we know the file input has a selected file.

Conclusion

To validate a file upload field using JavaScript, we can check the value property.