2

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>

This code is for validating the from date and to date.Validation is happening but after validation the form gets submit again.This is used in search report function,so if the entered to date is less than from date it is showing error message and it go's for search ,which should not happen.

Can any one tell me what would be the problem

1

3 Answers 3

2
+50

we have different solution for this problem, you can try it

<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>
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this? https://docs.djangoproject.com/en/dev/ref/validators/

In case you want to make a validation on multiple fields you can do so in the form's clean method,so in your search form you'd have:

class SearchFilterForm(Form):

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

then in your view you will have:

if form.is_valid():
    ...

and in your template you can use:

{{ form.non_field_errors }}

Or instead of raising ValidationError you can manually set a field error like so:

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

and in the template you can use:

{{form.todate.errors}}

5 Comments

followed the same procedure getting error"'SearchFilterForm' object has no attribute 'fromdate'"
ah sorry, it should be self.cleaned_data['fromdate'] I believe
see what i tried def clean(self): fromdate=self.cleaned_data['fromdate'] todate=self.cleaned_data['todate'] if fromdate < todate: raise ValidationError('Please correct the To date') return self.cleaned_data,no validation is happening
are you using from.is_vaild() anywhere in your view?
Yes,i am using searchform.is_valid(): in views.py
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>

Modified:

<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>

3 Comments

I checked your answer,validation is not working,button click is not happening,in console i got this error "Uncaught TypeError: Cannot set property 'disabled' of null "
hi please check my modified code along with html. its working now i think. you may have to modify the code based on your html code ..
I already using a datepicker with date field,in html you again initialized the field for date field and i tried your code as the same ,submit button onclick is not happening.No errors in console i don't know whether it is working in html file or not because i directly applied the answer in my app using django framework.Will check in other html page revert you back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.