在django比较日期和验证]

时间:2013-06-21 15:44:57

标签: javascript django django-forms django-templates django-views

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 comparedate(){
        var fromdate = document.getElementById("id_fromdate").value;
        var todate = document.getElementById("id_todate").value;
        if(fromdate<todate){
        {
        $("#error-warning").show();
        $("#error-warning").text("Please correct the To date");          
        return false;        
    }

 }

template.html

<button type="submit" name="filter" onclick="comparedate()">Go <img src="/static/images/button-icon-ir-fwd.png" alt="" height="17" width="8"></button><div id="error-warning" style="display:none" class="errorlist">Please correct the To date</div>

此代码用于验证日期和日期。验证正在进行,但验证后表单再次提交。这用于搜索报告功能,因此如果输入的日期小于日期,则显示错误消息,它是搜索,这不应该发生。

任何人都可以告诉我会出现什么问题

3 个答案:

答案 0 :(得分:2)

我们对此问题有不同的解决方案,您可以尝试一下

<button type="submit" name="filter" onclick="javascript:return comparedate();"> Go<img src="/static/images/button-icon-ir-fwd.png" alt="" height="17" width="8"></button><div id="error-warning" style="display:none" class="errorlist">Please correct the To date</div>

答案 1 :(得分:0)

document.getElementById("button_id").disabled = true

$("id_todate").change(function(){ 
    var fromdate = document.getElementById("id_fromdate").value;
    var todate = document.getElementById("id_todate").value;

    if (Date.parse(fromdate) > Date.parse(todate)) { 
        $("#error-warning").hide();
        document.getElementById("button_id").disabled=false;   
    }else{
        $("#error-warning").show();
        $("#error-warning").text("Please correct the To date"); 
    }
}

<button type="submit" name="filter" id="button_id" disabled>Go <img src="/static/images/button-icon-ir-fwd.png" alt="" height="17" width="8"></button>
<div id="error-warning" style="display:none" class="errorlist">Please correct the To date</div>

修改:

<html>
    <script type="text/javascript" src="jquery-1.10.1.min.js" ></script>
    <input type="date" id="id_fromdate" />
    <input type="date" id="id_todate" />
    <input type="submit" name="filter" id="button_id" disabled />
    <div id="error-warning" style="display:none" class="errorlist">Please correct the To date</div>
    <script type="text/javascript">
        $("input[type='date']").change(function(){ 
            var fromdate = document.getElementById("id_fromdate").value;
            var todate = document.getElementById("id_todate").value;

            if (Date.parse(fromdate) > Date.parse(todate)) { 
                //console.log(fromdate)
                //console.log(todate)
                    $("#error-warning").hide();
                    document.getElementById("button_id").disabled=false;   
            }else{
                //console.log(fromdate)
                //console.log(todate)
            document.getElementById("button_id").disabled=true;   
                    $("#error-warning").show();
                    $("#error-warning").text("Please correct the To date"); 
            }
        })
    </script>

答案 2 :(得分:0)

你试过这个吗? https://docs.djangoproject.com/en/dev/ref/validators/

如果您想对多个字段进行验证,可以使用表单的clean方法进行验证,因此在搜索表单中您可以:

class SearchFilterForm(Form):

    def clean(self):
        if self.fromdate < self.todate:
            raise ValidationError('Please correct the To date')
        return self.cleaned_data

然后在您的视图中,您将拥有:

if form.is_valid():
    ...

在您的模板中,您可以使用:

{{ form.non_field_errors }}

或者不是提出ValidationError,而是可以手动设置字段错误,如下所示:

self._errors['todate'] = 'Please correct this'

并在模板中使用:

{{form.todate.errors}}