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}}* On Thursday, June 17, 2021 at 4:04:48 AM UTC-7 eugenet...@gmail.com wrote: > Dear NA, > > still it does not wark > > On Sun, 13 Jun 2021 at 03:20, Nikeet NA <nik...@exiverlabs.co.in> wrote: > >> 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) >> }) >> >> On Saturday, 12 June 2021 at 22:09:26 UTC+5:30 eugenet...@gmail.com >> wrote: >> >>> 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* >>> >>> >>> -- >> 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 >> >> <https://groups.google.com/d/msgid/django-users/d4c70f2f-2b3f-4d64-a94a-ee31b9ecdd03n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > *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 > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b31ba294-3c94-4358-9a7b-50c7f4bbf225n%40googlegroups.com.