Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
How to i add date validation to this javascript code using getfullyear

Create a function to validate the movie release year using the following business rules: • Release year must be in the range between 1930 to the current year

I want to add a validation to my code to validate year between 1930 and the current year and if it's not correct it should print an error

var movieTitle = [];
var movieReleaseYear = [];
function init() {
var add = document.getElementById("add");
add.onclick = () => addMovie();
var show = document.getElementById("show");
show.onclick = () => displayData();
}

function addMovie() {
var name = document.getElementById("movie");
var year = document.getElementById("year");
movieTitle.push(name.value);
movieReleaseYear.push(year.value);
}

function displayData() {
if (movieTitle.length == 0) {
document.getElementById("list").innerHTML = "there is no data";
} else {
var output = "";
movieTitle.forEach(
(element, index) =>
(output +=
" " + (index+1) + ". " + element + "\t\t" + movieReleaseYear[index] + "<br />")
);
document.getElementById("list").innerHTML = output;
}
}
window.onload = init;
<!DOCTYPE html>
<html lang="en">
<head>
 
 <title>Week 07 Pass Submission</title>
<script src="./w7p.js"></script>
</head>
<body>
<header>
<h1>Week 07 Pass Submission</h1>
</header>
<article>
<form>
<label for="movie">Movie title</label>
<input type="text" id="movie" /><br /><br />
<label for="year">Release year</label>
<input type="text" id="year" /><br /><br />
<input type="button" id="add" value="Add">
<input type="button" id="show" value="List All">
</form><br />
<div style="padding-left: 20px;" id="list"></div>

</article>
 
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

    <!DOCTYPE html>
    <html lang="en">
    <head>
     
     <title>Week 07 Pass Submission</title>
    <script src="./w7p.js"></script>
    </head>
    <body>
    <header>
    <h1>Week 07 Pass Submission</h1>
    </header>
    <article>
    <form>
    <label for="movie">Movie title</label>
    <input type="text" id="movie" /><br /><br />
    <span id="yearValidation" style="color: red"></span>
     <br />
    <label for="year">Release year</label>
    <input type="text" id="year" /><br /><br />
    <input type="button" id="add" value="Add">
    <input type="button" id="show" value="List All">
    </form><br />
    <div style="padding-left: 20px;" id="list"></div>
    
    </article>
     
    </body>
    </html>

var movieTitle = [];
var movieReleaseYear = [];
function init() {
  var add = document.getElementById('add');
  add.onclick = () => addMovie(2022);
  var show = document.getElementById('show');
  show.onclick = () => displayData();
}

function addMovie(currentYear) {
  var name = document.getElementById('movie');
  var year = document.getElementById('year');
  var year = document.getElementById('year').value;
  if (year <= 1930 || year >= currentYear) {
    document.getElementById('yearValidation').innerHTML = 'Error';
  } else {
    document.getElementById('yearValidation').innerHTML = '';
  }
  movieTitle.push(name.value);
  movieReleaseYear.push(year.value);
}

function displayData() {
  if (movieTitle.length == 0) {
    document.getElementById('list').innerHTML = 'there is no data';
  } else {
    var output = '';
    movieTitle.forEach(
      (element, index) =>
        (output +=
          ' ' +
          (index + 1) +
          '. ' +
          element +
          '\t\t' +
          movieReleaseYear[index] +
          '<br />')
    );
    document.getElementById('list').innerHTML = output;
  }
}
window.onload = init;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!