Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  166] [ 1]  / answers: 1 / hits: 72995  / 10 Years ago, fri, december 5, 2014, 12:00:00

I am absolutely new in JavaScript development and I have to perform the following task: I have 2 input tag containing 2 string representing date in the form 01/12/2014 (DAY/MONTH/YEAR). I use this input tag to search objects that have a date field that is among these dates.


<input type="hidden" size="9" class="impPrfTot" readonly="readonly"
onchange="cambioDataDa(this.value)"
name="dataDa" id="datada" value="<%=request.getSession().getAttribute("dataDa")%>"
style="font-size: 11px;color: #000000;">

<input type="hidden" size="9" class="impPrfTot" readonly="readonly"
onchange="cambioDataA(this.value); checkData(this.value)"
name="dataA" id="dataa" value="<%=request.getSession().getAttribute("dataA")%>"
style="font-size: 11px;color: #000000;">

<button class="dataDadataAButton" name="submitdataDadataA" onclick="sottomettiFormDataDaA(this)">Cerca</button>

And finally I have a button used to submit this form using this JavaScript:


function sottomettiFormDataDaA(obj) {
document.getElementById('dataDaAForm').submit();
}

What I need is to prevent that the value inside dataA (in English language dateTo) input is previous that dataDa value (in English language dateFrom).


I am trying to do something like this:



  1. The JavaScript is called at the onchange event on the change of a data and take the dataA string (tha represent the dateTo date) and check if it is previous of the dataA (the dateTo date).



  2. If the previous check is true the date range is invalid so the script show an error popup message and disallow the submit of the form having id="dataDaAForm"


    function checkData(dataA) {
    dataDa = document.getElementById('dataDa').value;

    if(dataDa > dataA){
    // SHOW A POPUP ERROR MESSAGE
    // DISALLOW THE FORM SUBMIT
    }
    }



Bue I have really not idea about complete this JavaScript and I don't know it exist better solution to achieve this task.


More From » jquery

 Answers
7

If you only need to compare the date without the time use this:





// convert date format to YYYY-MM-DD
var a = new Date().toJSON().slice(0, 10)
// get date from input field, by default is YYYY-MM-DD format
var b = document.getElementById('datePicker').value

// compare
console.log(a == b)
console.log(a > b)
console.log(a < b)

<input id=datePicker type=date />




[#68575] Wednesday, December 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;