In my application I want a customer to not press submit when he didn't change values in a specific form. I can do this serverside and add a viewmodelerror to the modelstate. But is there a way to do this also clientside with javascript? I searched for it, but couldn't find it.
In my application I want a customer to not press submit when he didn't change values in a specific form. I can do this serverside and add a viewmodelerror to the modelstate. But is there a way to do this also clientside with javascript? I searched for it, but couldn't find it.
Share Improve this question edited Nov 15, 2011 at 8:58 Prasanth 3,03133 silver badges44 bronze badges asked Nov 15, 2011 at 8:54 PatrickPatrick 2,7905 gold badges34 silver badges57 bronze badges 3- do you want to enable submit button only when a change has been done in the values ?? – Pankaj Upadhyay Commented Nov 15, 2011 at 9:06
- Yes, if that is possible. It would also be nice if it is possible to detect that the user has revert the change, but I can imagine that that is impossible. – Patrick Commented Nov 15, 2011 at 9:10
- i dont think it's impossible....Its how they prefer to do these things.....All you need is javascript function that detects for the changes on client side and then turns the submit button on – Pankaj Upadhyay Commented Nov 15, 2011 at 9:13
3 Answers
Reset to default 18You can set a javascript variable if the form is edited. A simple way of doing this would be to listen to the change event on input fields:
var isChanged = false;
$('input,select,textarea').change(function() {
isChanged = true;
});
And then check for isChanged
before submitting.
This approach doesn't deal with values being changed back to the original value though.
If you need to address that scenario, you would need to keep the form state in a javascript object and compare it with that.
You could add this to avoid the user to leave the page if the form has changed:
$(window).bind('beforeunload', function() {
if (isChanged) {
return 'You have changed the form, are you sure?';
} else {
return undefined;
}
});
Not sure what you asking? Are you talking about client side validation? in this case you could use MVC Validation or a javascript library like jquery and use a validation plugin
What is the client supposed to change?
If, for example. it's just the value of a text field you could save the original value in the document ready jquery event and when hitting the submit button run a javascript function that checks if the value has changed.
If you have more complex input fields you can still use this mechanism to save the original and check on submit trough javascript.