Allow field validation in ajax.

65 views
Skip to first unread message

Eugene TUYIZERE

unread,
Jun 12, 2021, 12:39:26 PM6/12/21
to django...@googlegroups.com
Friends,

The ajax codes below successfully save the records in the database without page loading. But what I want is to validate the fields first before saving. The codes do not validate the empty field. How can I include that validation ? please help

<script>
$(".submit_btn").click(function(){
var form=new FormData($("#ReportForm")[0]);
//AJAX CODE
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.status==200){
console.log(xhr.responseText);
}
}
xhr.open("POST","{% url 'report_create' %}",true);
$("#progressbar").show();

//UPDATING PROGRESS BAR
xhr.upload.addEventListener("progress",function(ev){
if(ev.lengthComputable){
var percentage=(ev.loaded/ev.total*100|0);
$("#progressbar").css({"width":""+percentage+"%"}).text("Uploading .."+percentage+"%");
console.log(percentage);
if(percentage>=100){
location.reload()

}
}
});

xhr.send(form);
})
</script>

Regards,
Eugene


Nikeet NA

unread,
Jun 12, 2021, 9:19:46 PM6/12/21
to Django users
You are not submitting your form which Inturn is not validating. For validations to run you need to submit a form.

Give the submit button attribute type="submit", then add an event listener for form submit in your javascript.
$("#ReportForm").on('submit', function(event) {
       // here event is submit type event object
       event.preventDefault() // this is added to prevent default execution of form submit event which will stop the page reload. 
       // here event.target is your form so you can use that to get data as let data = new FormData(event.target) 
})

Eugene TUYIZERE

unread,
Jun 17, 2021, 7:04:48 AM6/17/21
to django...@googlegroups.com
Dear NA,

still it does not wark

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d4c70f2f-2b3f-4d64-a94a-ee31b9ecdd03n%40googlegroups.com.


--
TUYIZERE Eugene

Msc Degree in Mathematical Science

African Institute for Mathematical Sciences (AIMS Cameroon)
Crystal Garden-Lime, Cameroon


Bsc in Computer Science

UR-Nyagatare Campus

Email: eugene....@aims-cameroon.org
           eugenet...@gmail.com

Tel: (+250) 7 88 26 33 38, (+250) 7 22 26 33 38

Ayush Bisht

unread,
Jun 17, 2021, 3:08:38 PM6/17/21
to Django users
If you specifically trying to validate the form on the basis of empty input field, then simply include  required attribute in you input field
 <input type="text" id="username" name="username" required>
and for more validation , you just validate all the thing in HTML only using django template tags... 
here I am including a descent approach ... 

     <form method = "POST" >{% csrf_token %}
            {% load bootstrap_tags %} 
           {{form.non_field_errors}}
           <!-- {{ form|as_bootstrap }}       -->
           <div class = "profile-root-inner"> 
             <ul class="profile-form-field">                        
                   
               <li>
                 <ul>  
                                     
                      {% if form.username.errors %}                            
                      <li>{{form.username.errors}}</li>      
                      {% endif %}
                      <li><label for="{{ form.userame.id_for_label}}">Username</label></li>                  
                       <li>{{form.username}}</li>
                     </ul>  
               </li> 

               <li>
                 <ul>                
                   {% if form.email.errors %}                            
                   <li>{{form.email.errors}}</li>      
                   {% endif %}
                   <li><label for="{{ form.email.id_for_label}}">Email</label></li>                   
                      <li>{{form.email}}</li>
                     </ul>  
               </li> 

                   <li>
                     <ul>   
                           {% if form.password1.errors %}                            
                               <li>{{form.password1.errors}}</li>      
                             {% endif %}
                           <li><label for="{{ form.password1.id_for_label}}">Enter the Password</label></li>                         
                          <li>{{form.password1}}</li>
                     </ul>  
                   </li> 

                   <li>
                     <ul>           
                       {% if form.password2.errors %}     
                         <li>{{form.password2.errors}}</li>
                       {% endif %} 
                         <li><label for="{{form.password2.id_for_label}}">Confirm Password</label></li>
                         <!-- <li><input class= "profile-input-field" placeholder="{{blogger.user_email}}" name = "user_email" type = "email" ></li> -->
                         <li>{{form.password2}}</li>
                     </ul>  
                   </li>
                                 
                   <li>
                     <ul>    
                       {% if form.captcha.errors %}     
                         <li>{{form.captcha.errors}}</li>
                       {% endif %}             
                          <li><label for="{{form.captcha.id_for_label}}">Captcha</label></li>                        
                       <li>{{form.captcha}}</li>
                       </ul>  
                     </li>                    
            </div>
               <button type = "submit" class="btn btn-success">Register</button>
            </form>


if any validation error has caught , then it will shown at {{form.captcha.errors}}




Reply all
Reply to author
Forward
0 new messages