Is there any way to check whether form is dirty or not in JavaScript?

Kumaresan Sd picture Kumaresan Sd · Oct 18, 2018 · Viewed 7.4k times · Source

I have one form validator example in java script. In this case, Is there any way to check whether the form is dirty or not?

My platform is JavaScript

Please find corresponding sample below, and suggest any solution.

sample link

code snipet: i have used like:

Answer

simonecosci picture simonecosci · Oct 24, 2018

you have tags as kendo-ui-grid and kendo-validator so I suppose you are using kendo framework. if you want to see if the form is dirty you should check the viewModel in kendo way sample.

basically I've created a viewModel which is impements the ObservableObject interface and has a two way binding with the form's container. Every time you change something in the form the change event is fired in the viewModel which set a variable (dirty) as true.

var dirty = false;
var viewModel = new kendo.data.ObservableObject({
    fullname: "test"
});
viewModel.bind("change", function () {
    alert("changed");
    dirty = true;
});
kendo.bind($("#tickets"), viewModel);

Add all the fields you need to "watch" in the ObservableObject and set on their markup the property data-bind="value:fieldName"