Saturday, June 12, 2021

Allow field validation in ajax.

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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABxpZHtHocb924Ka2hmvb1a3OveouwYKS-K2m_38gtsqY-4vkQ%40mail.gmail.com.

No comments:

Post a Comment