How to create HTML DOM Input type date ‘min’ and ‘max’ restriction using JavaScript ?- Example-2 Type Your Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Dynemic minimum date restriction using JavaScript</title> </head> <body> <p>Example of restricting date using JavaScript 'Min' attributes value</p> <label for="date">Select Date : </label><input type="date" id="date" > <script> var n = new Date(); //date constructor var y = n.getFullYear(); //current full year var m = n.getMonth()+1; //current month var d = n.getDate(); //current day //padStart fix length of sting as per requirement, for e.g. when month = 8 then month become 08 var today = y.toString().padStart(4, 0)+"-"+m.toString().padStart(2, 0)+"-"+d.toString().padStart(2, 0); //current date var elem = document.getElementById("date"); //HTML DOM input type date function restric(e){ e.value = today; e.setAttribute("min",today); } restric(elem); //function called </script> </body> </html> Result
0 comments: