admin管理员组

文章数量:1384555

Hai i am developing application django where in i want to validate fromdate is less than todate.I am using the following code but it is not working

forms.py

class SearchFilterForm(Form):

fromdate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yy','class':'datefield','readonly':'readonly'}))
todate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yy','class':'datefield','readonly':'readonly'}))

javascript:

function paredate(){
        var fromdate = document.getElementById("id_fromdate");
        var todate = document.getElementById("id_todate");
        if(fromdate<todate){
            alert("Start date should be less than end date");
        return false;
    }
}

I am getting the input from date picker and the format is yy-mm-dd.The above code is not giving any error in console but it is not working.

Hai i am developing application django where in i want to validate fromdate is less than todate.I am using the following code but it is not working

forms.py

class SearchFilterForm(Form):

fromdate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yy','class':'datefield','readonly':'readonly'}))
todate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yy','class':'datefield','readonly':'readonly'}))

javascript:

function paredate(){
        var fromdate = document.getElementById("id_fromdate");
        var todate = document.getElementById("id_todate");
        if(fromdate<todate){
            alert("Start date should be less than end date");
        return false;
    }
}

I am getting the input from date picker and the format is yy-mm-dd.The above code is not giving any error in console but it is not working.

Share Improve this question asked Jun 20, 2013 at 14:45 Monk LMonk L 3,3689 gold badges28 silver badges42 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

document.getElementById("id_fromdate") you are retrieving the node. What you need is the value

var fromdate = document.getElementById("id_fromdate").value;
var todate = document.getElementById("id_todate").value;

Then, you need to pare the dates, not just the string that .value would retrieve.

var fromdate = document.getElementById("id_fromdate").value;
var todate = document.getElementById("id_todate").value;

You need to convert date value string date object then you can pare two date object

http://www.w3schools./jsref/jsref_obj_date.asp

Ex. var d1 = new Date();

var d2 = new Date();
if(+d1>+d2)

Try this

if (Date.parse(fromDate) > Date.parse(toDate)) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!")
return false;
}

本文标签: javascriptcompare from date is less than to dateStack Overflow